I want to know if in this JSON:
"salmos": [{
"orden": "1",
"antifona": "Han llegado los días de penitencia; expiemos nuestros pecados y salvaremos nuestras almas.",
"ref": "Salmo 118, 17-24",
"tema": "NULL",
"intro": "NULL",
"parte": "NULL",
"salmo": "Haz bien a tu siervo: viviré_y cumpliré tus palabras;_ábreme los ojos y contemplaré_las maravillas de tu voluntad;_soy un forastero en la tierra:_no me ocultes tus promesas.§Mi alma se consume, deseando_continuamente tus mandamientos;_reprendes a los soberbios,_infelices los que se apartan de tus mandatos;_aleja de mí las afrentas y el desprecio,_porque observo tus preceptos.§Aunque los nobles se sientan a murmurar de mí,_tu siervo medita tus leyes;_tus preceptos son mi delicia,_tus decretos son mis consejeros."
}, { ...
values that appear like this: "NULL"
, would be interpreted as a string whose value isNULL
, or would be interpreted as the value NULL
itself .
The doubt came to me because I don't know if I could have problems when retrieving any of those values, which can be a valid string, or an empty string that I wanted to represent as NULL
for greater security.
I can comment you with an example;
JSON1 {} : Returns an empty object.
JSON2 {"myCount": null}: This is the definitive way to represent nulls according to the official documentation http://www.json.org/
JSON4 : Returns an empty string.
JSON5 {"myString": "null"}: With this you will have a literal string that contains NULL as a string.
I got this info some time ago from this post in English: https://stackoverflow.com/questions/21120999/representing-null-in-json
In the second edition of the final draft for the JSON syntax standard (dated December 2017), in section 5 of Values in JSON (page 10 in the linked PDF) it is stated that (my translation):
Therefore
null
(lowercase) would be allowed as a value and would be most appropriate for defining null values in JSON.In the case exposed in the question, the values will not be interpreted as
null
but as the text string "NULL" which could cause problems because being a non-empty string it will be evaluated astrue
in a conditional while the valuenull
evaluates asfalse
, being able to generate unexpected behaviors. For example:More than worrying about how to represent the values in the .json, in this case it would be how to represent them in the application and that it works correctly when containing this type of value.
You can add the values however you want to represent them in your .json, for example:
or simply not add it, since your parser is the one in charge of representing those values in the objects that your application will use, obviously you have to validate when these values do not exist in the .json.
From experience the best approach to handle this type of values is not to add value or not to add the property when it has a null value, one of the reasons is:
by not reading these properties you won't have to add them to the object: