4

How would you get a JSONPath to all child node of an object?

E.g.:

var data = [{
    "key1": {
        "children": [{
            "key2": "value",
            "key3": "value",
            "key4": {}
        }, {
            "key2": "value",
            "key3": "value",
            "key4": {}
        }],
        "key5": "value"
    }
}, {
    "key1": {
        "children": {
            "key2": "value",
            "key3": "value",
            "key4": {}
        },
        "key5": "value"
    }
}]

I want to get absolute path for all nodes in the data structure as an array:

[
    "data[0]['key1']['children'][0]['key2']", 
    "data[0]['key1']['children'][0]['key3']", 
    "data[0]['key1']['children'][0]['key4']", 
    ......, 
    "data[0]['key1']['children'][1]['key2']",
    ......., 
    "data[1]['key1']['children']['key2']",
    ..........
]

Is there any way to get this done in JS?

Okky
  • 10,338
  • 15
  • 75
  • 122
  • Yes, I suppose there are many ways to do it... Where did you get stuck? – elclanrs Sep 05 '13 at 06:40
  • Ok, I understand the term "JSONPath" (based on the tag description, it makes sense (the comparison between to XPath and XML seems appropriate)). But you are not working with JSON in your example, hence I will edit your question accordingly. – Felix Kling Sep 05 '13 at 06:42
  • I'm confused in how to right the recursive function. – Okky Sep 05 '13 at 06:42
  • Does this help? https://code.google.com/p/jsonpath/ – elclanrs Sep 05 '13 at 06:43
  • You might want to have a look at http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json. – Felix Kling Sep 05 '13 at 06:47
  • @elclanrs I'm looking for some simple custom code. Will try this too – Okky Sep 05 '13 at 06:49
  • @FelixKling: I Can write it manually, but I was looking for a code that generates array of JSON path when JSON array is given as input – Okky Sep 05 '13 at 06:53
  • There is no native function that does that. You have to write your own or use an existing external library. Regarding your edit, [there is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). You are confusing object literals with JSON. – Felix Kling Sep 05 '13 at 06:54
  • @FelixKling: I have to write recursive function right? I'm too weak in that :) – Okky Sep 05 '13 at 06:56
  • That's why I said have a look at the other question. It provides to examples of recursive functions to traverse nested data structures. – Felix Kling Sep 05 '13 at 06:56

2 Answers2

5

I wrote a custom code that gives us JSON path of all nodes as array

function toArray(obj, name) {
    var result = [];
    var passName;
    var tempArray = [];
    for (var prop in obj) {
        var value = obj[prop];
        if (typeof value === 'object') {
            if ($.isNumeric(prop)) {
                passName = name + "[" + prop + "]";
            } else {
                passName = name + "['" + prop + "']";
            }
            tempArray = toArray(value, passName);
            $.each(tempArray, function (key, value) {
                result.push(value);
            });

        } else {
            result.push(name + "['" + prop + "']");
        }
    }
    return result;
}

JS Fiddle

Okky
  • 10,338
  • 15
  • 75
  • 122
3

well , I think you have to loop through your entire json structure..

  for(var i=0; i<data.length; i++) {
     for(var j in data[i]){
       //and so on
      }
   }

Or simply you can create aleas arrary for every key where key will be the path of that value with "_" as separator like..

 var aleasArray = []; 
 for(var i=0; i<data.length; i++) {
     for(var j in data[i]){
      aleasArray[i + '_' + j] = data[i][j];  // if you want make it more generalize aleas array..
      }
   }

hope this will help you..

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42