0

This is the JSON array:

    {
  "head": {
    "status": "200",
    "data": {}
  },
  "body": {
    "content": {
      "deliveryMessage": {
        "MT9J2AE/A": {
          "label": "Delivers:",
          "quote": "Sep 23-Sep 24",
          "address": {
            "city": "Ajman"
          },
          "showDeliveryOptionsLink": false,
          "messageType": "Delivery",
          "basePartNumber": "MT9J2",
          "commitCodeId": "8886",
          "idl": false,
          "defaultLocationEnabled": false
        },
        "MT9M2AE/A": {
          "label": "Delivers:",
          "quote": "25 Sep - 2 Oct",
          "address": {
            "city": "Ajman"
          },
          "showDeliveryOptionsLink": false,
          "messageType": "Delivery",
          "basePartNumber": "MT9M2",
          "commitCodeId": "199",
          "idl": false,
          "defaultLocationEnabled": false
        },
        "geoLocated": false,
        "deliveryLocationLink": {
          "text": "Ajman",
          "dataVar": {},
          "newTab": false
        },
        "dudeCookieSet": true,
        "MT9F2AE/A": {
          "label": "Delivers:",
          "quote": "25 Sep - 2 Oct",
          "address": {
            "city": "Ajman"
          },
          "showDeliveryOptionsLink": false,
          "messageType": "Delivery",
          "basePartNumber": "MT9F2",
          "commitCodeId": "199",
          "idl": false,
          "defaultLocationEnabled": false
        },
        "deliveryLocationLabel": "Your delivery location:",
        "WarmAPUWithDude": true,
        "locationCookieValueFoundForThisCountry": true,
        "dudeLocated": true,
        "accessibilityDeliveryOptions": "delivery options",
        "little": true
      }
    }
  }
}

I want to access the three "quote" attribute's values.

So far this is what I have achieved:

parsedata = JSON.parse(jsondata);
parsedata[0].body.content.deliveryMessage["MT9J2AE/A"].quote

This will give the "quote" value of "MT9J2AE/A". But if that attribute changes, how do I get the quotes?

I mean I am asking something like this:

parsedata[0].body.content.deliveryMessage[0].quote

But that is not working :(

  • 2
    That JSON contains no arrays – Quentin Sep 16 '18 at 17:48
  • How do you want to distinguish which object's `quote` you need? You can iterate thorough `deliveryMessage` properties and pick one, but how you'll be sure that it's the object you want? – Papi Sep 16 '18 at 17:50
  • 1
    You can use Object.keys to iterate over keys of the Object – AvcS Sep 16 '18 at 18:00
  • 1
    Thanks @AvcS Got this: var objectdata = Object.keys(parsedata[0].body.content.deliveryMessage)[0]; parsedata[0].body.content.deliveryMessage[objectdata].quote; now I can further manipulate using loops and conditions. – Umair Mehmood Sep 16 '18 at 18:28

0 Answers0