1

Below is my json which is dynamic. I want to access 'bf' key in the json , 'xxxxxx20160929' and 'yyy813AI20160929' keys are dynamic but json structure will be the same

{
 "resultData": [
 {
 "a": "124",
 "b": "0",
 "c": "0",
 "flc_schedu": {
 "e": "6",
 "f": "en",
 "xxxxxx20160929": [
 {"ID": "yyyyyyyy" },
 {"ID": "fffff"}
 ]
 },
 "fareDetails": {
 "xxxxxx20160929": {
 "yyy813AI20160929": {
 "O": {
 "AD": {
 "bf": "2527"
 }
 }
 }
 }
 }
 }
 ]
}

Below is how I tried

response.resultData[0].fareDetails[Object.keys(response.resultData[0].fareDetails)[0]]

If I try as above I can able to access dynamically up to "xxxxxx20160929" key, but I can't able get how to reach up to "bf" key dynamicaly.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Sep 24, 2016 at 13:40
5
  • you just need to append .yyy813AI20160929.O.AD.bf Commented Sep 24, 2016 at 13:45
  • @Pat : yyy813AI20160929 key is dynamic, its not possible to do like u given Commented Sep 24, 2016 at 13:46
  • Try this: data.resultData[0].fareDetails.xxxxxx20160929['yyy813AI20160929'].O.AD.bf Commented Sep 24, 2016 at 13:48
  • @Samuel : : yyy813AI20160929 key is dynamic, we cannot keep it static as u given Commented Sep 24, 2016 at 13:49
  • that is simple - you can do this var data = <<< access dynamically up to "xxxxxx20160929" >>> then do this (provided there is only 1 child agreed for xxxxxx20160929) var value; for (var key in data) { value = key; } thenyou have your bf value - data.value.O.AD.bf Commented Sep 24, 2016 at 14:03

3 Answers 3

1

You can reference an object using the array syntax.

var one = 'xxxxxx20160929';
var two = 'yyy813AI20160929';
data.resultData[0].fareDetails[one][two].O.AD.bf;

UPDATE:

This code assumes there is only one dynamic object at each layer.

var one = Object.keys(data.resultData[0].fareDetails)[0];
var two = Object.keys(data.resultData[0].fareDetails[one])[0];
var thing = data.resultData[0].fareDetails[one][two].O.AD.bf;
answered Sep 24, 2016 at 13:52
Sign up to request clarification or add additional context in comments.

5 Comments

: xxxxxx20160929, yyy813AI20160929 key are dynamic, we cannot keep it static as u given
Can you sugget how to access "yyy813AI20160929" dynamically
Do you mean you don't know what the property is named?
i undestood above way but u kept 'two' variable static and taking 'yyy813AI20160929', i want to know how to take 'yyy813AI20160929' value dynamically from above json
I updated the answer to show how to get the key dynamically for each layer.
1
function getBFFromFareDetails(details){
 var bfValues = [];
 for(var k in details.fareDetails){
 // loop over the children of fareDetails
 if( details.fareDetails.hasOwnProperty( k ) ) {
 //each entry in ;fareDetails'
 var itemRoot = details.fareDetails[k]
 for(var k1 in itemRoot){
 // loop over the children of the first unknown item
 if( itemRoot.hasOwnProperty( k1 ) ) {
 //return the bf from the first unknown child
 return itemRoot[k1].O.AD.bf;
 }
 }
 } 
 }
}

If you call this with var bf = getBFFromFareDetails(response.resultData[0]) this will return the value for the first bf in the first child of fareDetails and its first child.

You can see a quick example in action here https://jsfiddle.net/tocsoft/5364x2sp/

answered Sep 24, 2016 at 14:09

Comments

0

If you are able to access up to "xxxxxx20160929" level then create a var to store that level, then use that variable to access the next which you will need to store in a variable, then use both variable to access the key needed.

var1 = response.resultData[0].fareDetails)[0];
var2 = response.resultData[0].fareDetails)[0][var1];
response.resultData[0].fareDetails)[0][var1][var2];
answered Sep 24, 2016 at 14:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.