0

I have been working with complex,nested JSON file as given below:

Update:: Nested JSON file snippet (example)

{
    "sample": {
        "someitem": {
            "thesearecool": [
                {
                    "neat": "wow"
                },
                {
                    "neat": "tubular"
                }
            ]
        },
        "coolcolors": [
            {
                "color":"red",
                "hex":  "ff0000"
            },
            {
                "color":"blue",
                "hex":"0000ff"
            }
        ]
    }
}

I want to traverse through each and every value in this JSON file. I have tried many npm nodejs packages to make nested JSON into plain, readable JSON format.(NPM Packages-> flattenr, flat, etc.).

Kindly someone help me to resolve this issue. Please give some better solutions with examples.

deraazSk
  • 21
  • 4
  • I don't see any nested JSON. Note JSON is just textual representation of a javascript object/primitive. So in order for you to have a nested JSON it would be like `{"json":"{\"key\":\"value\"}"}`. Do you mean child objects/properties? – Patrick Evans Nov 24 '16 at 13:14
  • Whatever , I think you have understood what I want to say. As I cannot post organizational data. Please suggest me that how to read Nested json file, in simple key value pair. – deraazSk Nov 24 '16 at 13:20

1 Answers1

1

Yeah, that's a nice problem of recursion. So, the general idea is a function with a for-loop.

2 things can happen: either it's a value, then you print it. Or it's a object, then you put that object through the same function.

<div id="log"></div>
<script>
var data = {
"sample": {
    "someitem": {
        "thesearecool": [
            {
                "neat": "wow"
            },
            {
                "neat": "tubular"
            }
        ]
    },
    "coolcolors": [
        {
            "color":"red",
            "hex":  "ff0000"
        },
        {
            "color":"blue",
            "hex":"0000ff"
        }
    ]
}
};

function readAllJson(data, level) {
  var resultString = '';
  for(var i in data) {
    var type = typeof data[i];
    switch(type) {
      case 'object':
        resultString += indent(level) + i +':<br/>'+ readAllJson(data[i], level + 1);  // recursion
        break;
      default: 
        resultString += indent(level) + i +': '+ data[i] + '<br/>';
      break;
    }
  }
  return resultString;
}
function indent(level) {
  var result = '';
  for(var i=0; i<level; i++) {
    result += '&nbsp;';  // HTML space character
  }
  return result;
}
window.onload = function() {
  var log = document.getElementById('log');
  var result = readAllJson(data, 0);
  log.innerHTML = result;
}
</script>
Emmanuel Delay
  • 3,619
  • 1
  • 11
  • 17
  • @ Emmanuel, Thanks for your resolution comments. However, could you please give some more simplified way to iterate through the Nested Json values. NOTE: I am using a full-fledged purely javascript coding, hence it will be great if you post some examples that aren't inside HTML or Script tags. Examples with pure javascript is expected. – deraazSk Nov 25 '16 at 05:42
  • Recursion is hard. It is unintuitive. Usually things happen backwards (what is processed first is often printed last), so it's not that easy to explain. I'm not sure what you are asking me. Tell me specifically what end result you would expect, let's work towards that. – Emmanuel Delay Nov 25 '16 at 14:09