Problems reading JSON with nested objects/arrays

Hi,
How do I parse the following JSON output from an API call. I have managed to loop through the highest level of the json with a foreach and can get objects with their “id” values. My problem is how to loop through the “Answers” level to read the “name” value. I have tried a nested foreach loop but just can’t make it work.

{
"responseCode": 200,
    "message": "success",
    "content": [
        {
            "id": "5177324193621660980",
            "form_id": "220077009162952",
            "ip": "62.92.26.3",
            "created_at": "2022-01-11 12:47:00",
            "updated_at": null,
            "answers": {
                "1": {
                    "name": "clickTo",
                    "order": "1",
                    "text": "Søknad om å opprette et Team",
                    "type": "control_head"
                },
                "2": {
                    "name": "submit",
                    "order": "10",
                    "text": "Send inn",
                    "type": "control_button"
                },
                "5": {
                    "name": "Begrunnelse",
                    "order": "5",
                    "text": "Hva er begrunnelsen for at du trenger et team? ",
                    "type": "control_textarea",
                    "answer": "Oppgradering av nettverk"
                },

            }
        },
        {
            "id": "5177311423624123388",
            "form_id": "220077009162952",
            "ip": "62.92.26.3",
            "created_at": "2022-01-11 12:25:43",
            "updated_at": null,
            "answers": {
                "1": {
                    "name": "clickTo",
                    "order": "1",
                    "text": "Søknad om å opprette et Team",
                    "type": "control_head"
                },
                "2": {
                    "name": "submit",
                    "order": "10",
                    "text": "Send inn",
                    "type": "control_button"
                },
                "5": {
                    "name": "Begrunnelse",
                    "order": "5",
                    "text": "Hva er begrunnelsen for at du trenger et team? ",
                    "type": "control_textarea",
                    "answer": "Prosjekt sikkerhet 2022"
                },

            }
        }
    ],
    "duration": "14.37ms",
    "resultSet": {
        "offset": 0,
        "limit": 20,
        "count": 3
    }
}

Please keep it simple. Thanks

Kjellmagne,
Welcome to the forum. :wave:t4:

Before we proceed - please go back and edit your question to fix the formatting of your JSON code.

To post code, error messages, sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

And please show the code you have so far. So we can relate to it and don’t have to start from scratch. :wink:

I’m not sure if you posted invalid Json or if the formatting screwed it up but I took the liberty of simplying your Json file to give you some example code.

Once you’ve converted the Json, you can work with it like any other object, using dot notation:

$content = @"
{
    "responseCode": 200,
    "message": "success",
    "content": [
        {
            "id": "5177324193621660980",
            "form_id": "220077009162952",
            "ip": "62.92.26.3",
            "created_at": "2022-01-11 12:47:00",
            "updated_at": null,
            "answers": {
                "1": {
                    "name": "clickTo",
                    "order": "1",
                    "text": "Søknad om å opprette et Team",
                    "type": "control_head"
                },
                "2": {
                    "name": "submit",
                    "order": "10",
                    "text": "Send inn",
                    "type": "control_button"
                },
                "5": {
                    "name": "Begrunnelse",
                    "order": "5",
                    "text": "Hva er begrunnelsen for at du trenger et team? ",
                    "type": "control_textarea",
                    "answer": "Oppgradering av nettverk"
                }
            }
        }
    ]
}
"@

$json = $content | ConvertFrom-Json

foreach ($answer in $json.content.answers) {
    $answer.PSObject.Properties.Value | Select Name
}

Output:

name       
----
clickTo
submit
Begrunnelse

Sorry about the formatting but I needed to truncate some the the JSON as it was too long.

Thanks for the answer, but how do I still know the “id” of the parent node/object? Initially , I want to loop through all the parent nodes and check whether “order” field under the “answers” node and item “5” is equivalent to a certain number value.

Your JSON data seems inconsitent … but if your JSON data always has the same structure you can use the so called dot notation to access the desired nodes:

$JSONData = @"
{
    "responseCode": 200,
    "message": "success",
    "content": [
        {
            "id": "5177324193621660980",
            "form_id": "220077009162952",
            "ip": "62.92.26.3",
            "created_at": "2022-01-11 12:47:00",
            "updated_at": null,
            "answers": {
                "1": {
                    "name": "clickTo",
                    "order": "1",
                    "text": "Søknad om å opprette et Team",
                    "type": "control_head"
                },
                "2": {
                    "name": "submit",
                    "order": "10",
                    "text": "Send inn",
                    "type": "control_button"
                },
                "5": {
                    "name": "Begrunnelse",
                    "order": "5",
                    "text": "Hva er begrunnelsen for at du trenger et team? ",
                    "type": "control_textarea",
                    "answer": "Oppgradering av nettverk"
                }
            }
        }
    ]
}
"@ | ConvertFrom-Json

$JSONData.content.id

$JSONData.content.answers.1

$JSONData.content.answers.2

$JSONData.content.answers.5