0

I'm trying to go through this JSON object and grab some values out of it.

let currentPage = "
{
    "sys": {
        "space": {
            "sys": {
                "type": "Link",
                "linkType": "Space",
                "id": "xaswoie0ncrg"
            }
        },
        "id": "7lqAYzwP92G9TMDBUVnadp",
        "type": "Entry",
        "createdAt": "2020-07-30T18:08:33.159Z",
        "updatedAt": "2020-07-30T18:22:50.276Z",
        "environment": {
            "sys": {
                "id": "master",
                "type": "Link",
                "linkType": "Environment"
            }
        },
        "revision": 2,
        "contentType": {
            "sys": {
                "type": "Link",
                "linkType": "ContentType",
                "id": "landingPage"
            }
        },
        "locale": "en-US"
    },
    "fields": {
        "pageTitle": "Leading the next generation of renewable fuels",
        "heroImage": {
            "sys": {
                "type": "Link",
                "linkType": "Asset",
                "id": "vnjfnYzSyhqOjKlmNmBGb"
            }
        },
        "pageZone": [
            {
                "sys": {
                    "type": "Link",
                    "linkType": "Entry",
                    "id": "3aQvORUYowW0SoofuvHUov"
                }
            },
            {
                "sys": {
                    "type": "Link",
                    "linkType": "Entry",
                    "id": "Qfj1hNJ9euSkBcAQEDaN5"
                }
            }
        ]
    }
}"

I then parse the JSON:

let currentPage2 = JSON.parse(currentPage);

Now here's the issue. If log this in the console:

console.log(Object.keys(currentPage2.fields.pageZone[0].sys.id));

Node returns this in the terminal:

[
  '0',  '1',  '2',  '3',  '4',
  '5',  '6',  '7',  '8',  '9',
  '10', '11', '12', '13', '14',
  '15', '16', '17', '18', '19',
  '20', '21'
]

I want to use this:

console.log(Object.keys(currentPage2.fields.pageZone[0].sys.id).value);
//with expected value of "3aQvORUYowW0SoofuvHUov"

Instead, it returns undefined. I have no idea why this is happening. I have tried using JSON.stringify etc and parsing it again, but it still behaves this way.

1
  • just use console.log(currentPage2.fields.pageZone[0].sys.id) Commented Aug 5, 2020 at 23:56

2 Answers 2

3

Just use currentPage2.fields.pageZone[0].sys.id. There is no need for Object.keys at all, unless you want each index of the string.

Sign up to request clarification or add additional context in comments.

2 Comments

God, simple error. I was using this for another purpose and ended up copying it. Thanks for that.
@AnthonySchanen No problem.
1

currentPage2.fields.pageZone[0].sys.id is a string, and since Object.keys will interpret the string as an iterable, the keys will be the indexes of each characters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.