i'm using json path parser parse data. here example of source data:
{ "people": [ { "initials": "rf", "sport": "t" }, { "initials": "lm", "sport": "f" } ], "definitions": { "rf": { "def_1": "roger federer", "def_2": "roger" }, "lm": { "def_1": "lionel messi", "def_2": "lionel" }, "t": { "def_1": "tennis" }, "f": { "def_1": "football" } } } now, if use array (people), it's useless, rf , t doesn't mean much. however, in object (definitions) defined. rf means roger federer , t tennis, need import 2 well. can import rf , t, i'm struggling find query connect rf , t roger federer , tennis.
first need specify context (root) , names of objects. here example:
context: $.[] query: initials result is: rf when import of in node, example works import array (people) data.
this can import now:
field1 value: rf field2 value: t what need:
field1 value: rf field2 value: t **field4 value: roger federer** **field5 value: tennis** basically, want describe better people array. 1 of combinations have tried, doesn't work:
context: $.[] query: initials.def_1 query: people.initials.def_1 is there way achieve this?
xpath sounds overkill such simple data structure. perhaps there other reasons need use xpath, easy in pure javascript. example:
var data = { "people": [ { "initials": "rf", "sport": "t" }, { "initials": "lm", "sport": "f" } ], "definitions": { "rf": { "def_1": "roger federer", "def_2": "roger" }, "lm": { "def_1": "lionel messi", "def_2": "lionel" }, "t": { "def_1": "tennis" }, "f": { "def_1": "football" } } }; data.people.foreach( function( person ) { var name = data.definitions[person.initials].def_1; var sport = data.definitions[person.sport].def_1; console.log( name, 'plays', sport ); }); logs:
roger federer plays tennis lionel messi plays football
Comments
Post a Comment