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

Get the id of an object in a json request?

So I have to get the first object of this list:

{
"2489": {
    "status": {
        "idstatus": "3",
        "status": "Sold"
    }
},

"2490": {
    "status": {
        "idstatus": "3",
        "status": "Sold"
    }
}

}

I don’t know beforehand the IDs [‘2489’] and [‘2490’]. Sometimes it responses only one of these IDs, sometimes 3 or more.

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

How can I get like [‘2490’].status.idstatus without knowing [‘2490’]?

>Solution :

There are several ways to do this. If you’re running a modern JS environment, you can use Object.keys or Object.values.

Assuming your JSON response object is a variable named res, you can construct a dynamic array of IDs like this:

const ids = Object.keys(res) // ["2489", "2490", ...]

If you don’t care about the IDs at all and want an array of all the inner objects, try this:

const items = Object.values(res) // [ { "status" : ... }, ... ]
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