How to get the keyword of a json varible?

Viewed 173

I have a varible, if I print it out, I see this output:

enter image description here

Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?

1 Answers

You could use the Object.keys function to retrieve all keys of your object.

The Object.keys returns an array, you can then access the first element of that array like any other array.

const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);

const keys = Object.keys(json);

console.log(keys[0]);

Related