0
{
    "1": {
        "emails": [
            {
                "address": "emailaddress@email.com",
                "verified": true
            }
        ],
        "_id": "dgBWJ4qBNsxa9a4MketL",
        "createdAt": "2019-07-23T15:33:34.270Z",
        "username": "emailaddress@email.com",
        "profile": {
            "active": true
        }
    }
}

i want to access "emailaddress@email.com" from the "address"

This is what i have tried

{ key: "emails[0].address", label: "Email" }
Akshay Mulgavkar
  • 1,727
  • 9
  • 22
  • Possible duplicate of [How to access specific object in JSON](https://stackoverflow.com/questions/54077334/how-to-access-specific-object-in-json) – Akshay Mulgavkar Aug 06 '19 at 06:33

3 Answers3

1

You missed getting the child with the key "1" of the outer JSON object.

Let's say after you parse this JSON string (jsonString) into a JS Object you get the following:

let jsonObject = parse(jsonString);
console.log(jsonObject['1']['emails'][0]['address']); // <== This is what you are looking for
Daniel Fuchs
  • 36
  • 1
  • 5
1

If you want to access the value, you shouldn't use quotation marks.

emails[0].address

Just like that to access the value, no quotation marks.

But it depends from what programming language did you use.

1

var objData = {
  "1": {
    "emails": [{
      "address": "emailaddress@email.com",
      "verified": true
    }],
    "_id": "dgBWJ4qBNsxa9a4MketL",
    "createdAt": "2019-07-23T15:33:34.270Z",
    "username": "emailaddress@email.com",
    "profile": {
      "active": true
    }
  }
}
var newOnbject = {};
var keys = Object.keys(objData);
for (var i = 0; i < keys.length; i++) {
  var key = keys[i];
  newOnbject.key = objData[key].emails[0].address;
  newOnbject.label = "Email"
}
console.log(newOnbject)

Note: Thou avoided receiving each child key of the external JSON objects.

Parth Raval
  • 4,097
  • 3
  • 23
  • 36