skip to Main Content

I am trying to drill down and extract values (in order to verify them) from a REST service response formatted as shown in the example below:

{
    "data": [
        {
            "A9001001A23D10A943E48481": {
                "id": "A9001001A23D10A943E48481",
                "type": "WIP",
                "status": "ERROR",
                "message": "could not move document",
                "details": [
                    "No document found with document id 'A9001001A23D10A943E48481'"
                ]
            }
        },
        {
            "A9001001A23D10A945468483": {
                "id": "A9001001A23D10A945468483",
                "type": "WIP",
                "status": "ERROR",
                "message": "could not move document",
                "details": [
                    "No document found with document id 'A9001001A23D10A945468483'"
                ]
            }
        }
    ],
    "error": {}
}

I need to compare actual values for id, type, status, message, and details with the expected values. I do know the id # but need to make sure the other values are correct in the response.

I can get the objects in the data[] array (data[0] and data[1]), but have failed completely to get anything below that. I’ve tried putting the array object into a map like so, but that doesn’t get me very far.

let myMap = new Map(Object.entries(jsonData.data[0]));

2

Answers


  1. In general you could use a loop to iterate through the array and accessing the
    properties
    of each object.

    const jsonData = {
      "data": [{
          "A9001001A23D10A943E48481": {
            "id": "A9001001A23D10A943E48481",
            "type": "WIP",
            "status": "ERROR",
            "message": "could not move document",
            "details": [
              "No document found with document id 'A9001001A23D10A943E48481'"
            ]
          }
        },
        {
          "A9001001A23D10A945468483": {
            "id": "A9001001A23D10A945468483",
            "type": "WIP",
            "status": "ERROR",
            "message": "could not move document",
            "details": [
              "No document found with document id 'A9001001A23D10A945468483'"
            ]
          }
        }
      ],
      "error": {}
    };
    
    jsonData.data.forEach((element) => {
      const key = Object.keys(element)[0]; // Get the key of the nested object
      const nestedObject = element[key]; // Access the nested object using the key
    
      console.log('Id:', nestedObject.id);
      console.log('Type:', nestedObject.type);
      console.log('Status:', nestedObject.status);
      console.log('Message:', nestedObject.message);
      console.log('Details:', nestedObject.details);
    });

    Would be great if you could extend your question with what result you are trying to get, like @Unmitigated commented.

    Login or Signup to reply.
  2. Here is some code you could try:

    let data=Object.values(jsonData.data[0]);
    // ^This is where you will be refering to the data [0] is for the first object
    // in this case, the first object is "A9001001A23D10A943E48481"
    console.log(data[0].id);
    // Here we are refering to the first object in the refered object above and calling the id on it
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search