0

I am trying to read a nested object as a key-value. I am using a recursive function to be able to enter each object and print its key-value. The problem is when I try to read a key-value where the value is null. It simply prints null, as if the key did not exist.

function readJsonObject(jsonObject){
                    for(var key in jsonObject){
                        if (typeof jsonObject[key] === 'object') {
                            readJsonObject(jsonObject[key]);
                        } else{
                            console.log(key + ": " + jsonObject[key]);
                        }
                    }
                };

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":null },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}'; 

var obj = JSON.parse(text);

readJsonObject(obj);

I should print:

firstName: John
lastName: null
firstName: Anna
lastName: Smith
firstName: Peter
lastName: Jones

But prints:

firstName: John
firstName: Anna
lastName: Smith
firstName: Peter
lastName: Jones
(Note that John's last name is not printed)

Any idea?

5
  • You should provide a minimal reproducible example (including sample input!) Commented Jan 21, 2019 at 14:16
  • Could you explain what you want your script to display instead of null ? Commented Jan 21, 2019 at 14:16
  • 5
    There's no sign of any JSON in here. JavaScript objects are not JSON. JSON is a textual data format. Commented Jan 21, 2019 at 14:17
  • The reason is that typeof jsonObject[key] === 'object' will be true also for null, so do typeof jsonObject[key] === 'object' && jsonObject[key] Commented Jan 21, 2019 at 14:24
  • Thanks @trincot, It was exactly what you said ! Commented Jan 21, 2019 at 14:43

1 Answer 1

1

Here is a sample of a function that prints all the key : value for all objects recursively

function readJsonObject(jsonObject) {

  if (Array.isArray(jsonObject)) {
    for (var el of jsonObject) {
      readJsonObject(el)
    }
    return
  } 

  else if (typeof jsonObject === 'object' && jsonObject.constructor === Object) {
    for (var key of Object.keys(jsonObject)) {
      var value = jsonObject[key];
      var toDisplay;

      if (value && typeof value === 'object' && value.constructor === Object) {
        toDisplay = readJsonObject(value);
      } else if (Array.isArray(value)) {
        toDisplay = JSON.stringify(value);
        readJsonObject(value);
      } else {
        toDisplay = value;
      }
      console.log(key + ": " + toDisplay);
    }
  }

  return jsonObject;
}

    
var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":null },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}'; 

var obj = JSON.parse(text);

console.log(readJsonObject(obj))

I handled Arrays as a special type, but please note there are other types that you maybe should check, since object encapsulate many other types, for instance null.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.