0

Trying to access JSON key value, but it gives me nothing .

Here is the code :

                    let json_=JSON.parse(JSON.stringify(result));
                    console.log(json_);
                    console.log(json_.tabs);
                    console.log(json_["tab"]);
                    console.log(json_['tab']);

Here is what I get :

json parsing problem

Here is a snap for detailed output :

JSON detailed output

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • Why stringify and immediately parse an object? – Pointy Jul 13 '19 at 13:42
  • The object printed to the console has only one key and that is neither `tabs` or `tab`. – Titus Jul 13 '19 at 13:45
  • It has, see [here](https://i.stack.imgur.com/tP2dY.png)... Whole string isn't showing here @Titus – Maifee Ul Asad Jul 13 '19 at 14:01
  • @Pointy I'm new in Java-script, can you please enlighten me – Maifee Ul Asad Jul 13 '19 at 14:01
  • If you've got a JavaScript object, there's usually no point in making it a JSON string and then immediately parsing it to convert it back to a JavaScript object. There are some circumstances in which that might make sense, but it's unusual. – Pointy Jul 13 '19 at 14:09
  • the console is not showing any error, what will you have in the variable `result` can you please mention here – Abhishek-Saini Jul 13 '19 at 14:27
  • The screenshot shows your object has one key that starts with MUA-S&S so to access the contents you need `json['MUA-S&S 2019...........1']` - the dots here should be replaced with the actual name of the key of course. – wOxxOm Jul 13 '19 at 14:40
  • @wOxxOm I have tried this `let json_=JSON.parse(JSON.stringify(result)); let i_json_=JSON.parse(JSON.stringify(json_)); let rr=i_json_['MUA-S&S 2019-07-13T14:44:32.711Z 1']; console.log(rr['id']);` *But no luck* – Maifee Ul Asad Jul 13 '19 at 14:52
  • 1
    Your value inside is a string (see the double quote marks), not an object. You should fix your code that generates the value. Start by removing all JSON.stringify and JSON.parse. – wOxxOm Jul 13 '19 at 14:59

2 Answers2

1

Ok, The thing is you have variable json_ whose value is an object when you do console.log(json_); the object gets print and the value of that object is:

{ MUA-S&S 2019...........1 : "{"alwaysOnTop"............................................382}"}

That is you have only one property inside the json object that is MUA-S&S 2019...........1 and the value of this key is"{"alwaysOnTop............................................382}" which is a string when you are trying to do :

console.log(json_.tabs) // undefined
console.log(json_["tab"]); // undefined
console.log(json_['tab']); // undefined

the value you get is undefined as your object does not have any property with name tabs,tab

Abhishek-Saini
  • 733
  • 7
  • 11
0

Here is the solution :

let tabs_=JSON.parse(result[Object.keys(result)[0]])['tabs'];

I got this from here.

And here is my project, if anyone is interested, feel free to contribute.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86