0

I'm calling an API in my application and need to drill down the JSON response. Within it there is a random string that changes so I can't hardcode the value. Is there a way I can access dataset1 no matter what the random string is?

{
    "data": {
        "Random String that Changes": { 
            "dataset1": {
                "date": {...}
            },
            "dataset2":{
                "date": {...}
            }
        }
    },
    "something else": {
        ...
    },
    "something else": {
        ...
    }
}

Previously I was hard coding the drill down like such:

this.props.data.randomData['Random String that Changes'].dataset1.date

Also tried:

this.props.data.randomData[0].dataset1.date
Stephanie Parker
  • 313
  • 3
  • 11

6 Answers6

2

You can get all the keys of the object using

const keys = Object.keys(this.props.data);

Now keys is an array of all keys , but if you json always only has 1 key, you can get your data using

this.props.data.randomData[keys[0]].dataset1.date

2

You can get dataset1

const values = Object.values(this.props.data)
console.log(values[0]['dataset1'])

Make sure that your json includes the "Random String that changes" at first place as shown in your above format.

Reference: Object.values

Danish
  • 443
  • 3
  • 13
1

Try accessing the object like this :

const obj = this.props.data;

obj[Object.keys(obj)[0]].dataset1.date

Reference: How to access the first property of an object in Javascript?

Nihal Saxena
  • 816
  • 3
  • 9
  • 20
1

Please check the below code for the solution.

var response = {
    "data": {
        "Random String that Changes": { 
            "dataset1": {
                "date": {...}
            },
            "dataset2":{
                "date": {...}
            }
        }
    },
    "something else": {
        ...
    },
    "something else": {
        ...
    }
};
var dataInRandomKey = response.data[Object.keys(response.data)[0]];

Now, you have the whole JSON object (in current example, response['data']['Random String that Changes']) in dataInRandomKey variable.

Nikhil Goyal
  • 1,821
  • 1
  • 6
  • 15
1
Consider sample data in myObj.

var myObj = {
    "data" : {
        "Random String that Changes": { 
          "dataset1": {
            "date": "123"
           },
          "dataset2":{
            "date": "123"
          }
        }
    }
  }


  var randomString =myObj[Object.keys(myObj)[0]];
  var dataset1Date =randomString[Object.keys(randomString)[0]].dataset1.date;
  console.log(dataset1Date);

So in this way you can access the date which you are trying with 
this.props.data.randomData['Random String that Changes'].dataset1.date
Sahit
  • 332
  • 3
  • 13
1

You can try for in loop

var a = {
"data": {
    "Random String that Changes": { 
        "dataset1": {
            "date": {...}
        },
        "dataset2":{
            "date": {...}
        }
    }
},
"something else": {
    ...
},
"something else": {
   ...
}
}

var response = a.data;

for(var key in response) {
    console.log(response[key].dataset1);
}