Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I retrive the value from closed JSON object?

The response object looks like this:

               {
                   "abc": [{
                              "xyz": "INFO 1",
                              "pqr": "INFO 2"
                            },
                            {
                              "xyz": "INFO 3",
                              "pqr": "INFO 4"
                          }]
                }

The expected value from this object

xyz, INFO 1
xyz, INFO 3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Could you help me to retrieve this value from the object?

>Solution :

  1. Convert JSON to JS object – JSON.parse(jsonString)
  2. Use standard dot notation and bracket notation to retrieve the values (e.g. JSobject.abc[0].xyz)
const object = {
  "abc": [{
      "xyz": "INFO 1",
      "pqr": "INFO 2"
    },
    {
      "xyz": "INFO 3",
      "pqr": "INFO 4"
    }
  ]
}

console.log(object.abc[0].xyz)

If you know the structure of the JSON you’re getting you can also iterate through it to get all the data at once:

const object = {
  "abc": [{
      "xyz": "INFO 1",
      "pqr": "INFO 2"
    },
    {
      "xyz": "INFO 3",
      "pqr": "INFO 4"
    }
  ]
}

object.abc.forEach(o => {
  Object.keys(o).forEach(p => {
    console.log(`${p}, ${o[p]}`)
  })
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading