0

I have been trying to access the elements of a nested object but still have no success. I searched through existing similar questions on stackexchange (eg: this) but could not resolve my issue. I tried to access the final element using console.log(result.final) but it shows undefined in the console. Kindly advise.

var data = '{"response":{"valid":true,"final":{"message":" MS02","tags":{"d1":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9","d2":"JhbGciOiJIUzI1NiJ9eyJ0eXAiOiJKV1QiLC","d3":"dupb9WWT8ypQYWw6QblkM98xFBBRsamkkWLw","d5":"5EChV1KJ4ASeh9crZDR3fivnSz4wCDmCr2RSC0CUrkx","d6":"hiH1I1SI3NHCYZeva0_FrjgSgxOa_YW6ECxRdAY-w5w","ua":"y"},"ti":"","op":[]}}}';
var dataJson = JSON.parse(data);
var result = [];

result = Object.entries(dataJson).map(([key, value]) => ({ [key]: value }))
console.log(result)
console.log(result.final)

UPDATE
I used typeof on dataJson and it showed it as a string so I did JSON.parse one more time on dataJson and then checked again with typeof which showed object and now after double JSON.parse I was able to access the nested values with dot operator (result.final), without mapping.

5
  • Try console.log(result[0].response.final)
    – danh
    Commented Aug 14, 2022 at 19:30
  • Is there a specific reason why you're mapping the dataJson to result, when you can access final just by using dataJson.response.final ? Commented Aug 14, 2022 at 20:19
  • @Pantalaimon No specific reason. I am unable to access value using ‘’’dataJson.response.final’’’
    – Crunch
    Commented Aug 15, 2022 at 3:45
  • @danh it didn’t work
    – Crunch
    Commented Aug 15, 2022 at 3:47
  • @Crunch - see the snippet. OP code exactly, plus console.log(result[0].response.final)
    – danh
    Commented Aug 15, 2022 at 6:00

3 Answers 3

1

You have been given multiple answers that work just fine based on the code that you've provided. Like I said in a comment earlier, you don't need to map the data to an array first before accessing its data.

var data = '{"response":{"valid":true,"final":{"message":" MS02","tags":{"d1":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9","d2":"JhbGciOiJIUzI1NiJ9eyJ0eXAiOiJKV1QiLC","d3":"dupb9WWT8ypQYWw6QblkM98xFBBRsamkkWLw","d5":"5EChV1KJ4ASeh9crZDR3fivnSz4wCDmCr2RSC0CUrkx","d6":"hiH1I1SI3NHCYZeva0_FrjgSgxOa_YW6ECxRdAY-w5w","ua":"y"},"ti":"","op":[]}}}';
var dataJson = JSON.parse(data);
console.log(dataJson.response.final);

If this doesn't work for you, then the information you've given might be incorrect or incomplete. Please provide for us a minimal reproducible example that clearly demonstrates the issue you're having.

2
  • Actually this piece of code exists inside success block of an ajax call. Data is received after the successful ajax call. console.log(result) shows we received data. But when I try to access as above, it didn’t work. Do i need to do something else for ajax success part?
    – Crunch
    Commented Aug 15, 2022 at 9:35
  • @Crunch Can you post the full code of the ajax call and the success method? Can you also verify that the value of the data variable is exactly the JSON that you've posted? My guess is that your JSON is somehow different than you expect it to be, if our solutions don't work for you. Commented Aug 15, 2022 at 9:42
0

Use:

console.log(result[0].response.final)

Not a js developer so maybe somebody can provide better solution

1
  • it didn’t work i tried it already
    – Crunch
    Commented Aug 15, 2022 at 3:46
0

The OP's code, changed only to log correctly...

var data = '{"response":{"valid":true,"final":{"message":" MS02","tags":{"d1":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9","d2":"JhbGciOiJIUzI1NiJ9eyJ0eXAiOiJKV1QiLC","d3":"dupb9WWT8ypQYWw6QblkM98xFBBRsamkkWLw","d5":"5EChV1KJ4ASeh9crZDR3fivnSz4wCDmCr2RSC0CUrkx","d6":"hiH1I1SI3NHCYZeva0_FrjgSgxOa_YW6ECxRdAY-w5w","ua":"y"},"ti":"","op":[]}}}';
var dataJson = JSON.parse(data);
var result = [];

result = Object.entries(dataJson).map(([key, value]) => ({ [key]: value }))
console.log(result)
console.log(result[0].response.final)

2
  • Actually this piece of code exists inside success block of an ajax call. Data is received after the successful ajax call. console.log(result) shows we received data. But when I try to access as above, it didn’t work. Do i need to do something else for ajax success part?
    – Crunch
    Commented Aug 15, 2022 at 7:01
  • Sorry I don't know how to help based on what's described here. The best path to fix is to debug (or log) at each line, stopping when you see something unexpected, then -- if necessary -- posting exactly that code, exactly what the log says, and how it differs from your expectation.
    – danh
    Commented Aug 15, 2022 at 16:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.