What is the criteria for representing null values ​​in JSON?

Question:

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."
            }, { ...

the values ​​that appear like this: "NULL" , would be interpreted as a string whose value is NULL or they would be interpreted as the NULL value itself .

The doubt came to me because I do not know if I could have problems when retrieving any of those values, which can be a valid string, or an empty string that I want to represent as NULL for greater security.

Answer:

I can tell you with an example;

    var json1 = '{}';
    var json2 = '{"myCount": null}';
    var json4 = '{"myString": ""}';
    var json5 = '{"myString": "null"}';
    
    console.log(JSON.parse(json1)); // {}
    console.log(JSON.parse(json2)); // {myCount: null}
    console.log(JSON.parse(json4)); // {myString: ""}
    console.log(JSON.parse(json5)); // {myString: "null"}

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 a while ago from this post in English: https://stackoverflow.com/questions/21120999/representing-null-in-json

Scroll to Top