Search code examples
jsonpowershellinvoke-webrequest

How to convert the result of invoke-request to a multidimensional array?


I used 'Invoke-request' to get the JSON bellow. I would like to convert it to a multi-dimensional array. I looked online but I can't find the appropriate solution. An answer would be great, if not, then point me to the appropriate resources would also help.

Thank you

{"ResultForName":
    [{"Name":"work","Values":["no"]},
    {"Name":"location","Values":["\\pc\\documents"]},
    {"Name":"Authority","Values":["10"]},
    {"Name":"City","Values":["Toronto"]},
    {"Name":"Country","Values":["CA"]},
    {"Name":"Created By","Values":["George"]},
    {"Name":"Money Vault Id","Values":["100000"]},
    {"Name":"Opened  On","Values":["Mark, John, L=Toronto, S=Ontario, C=CA","www.doman.com", O=Office, L=Toronto, S=Ontario, C="CA"]},
                    {"Name":"Driver Name","Values":["Honda"]},
                    {"Name":"Can drive","Values":["0"]},
                    {"Name":"Given Name","Values":["Steven"]},
                    {"Name":"Group Id","Values":["34324"]},
                    {"Name":"Internet System Address","Values"["www.doman.com/Locations"]},
                    {"Name":"Key value","Values":["AXU"]},
                    {"Name":"Key Encryption","Values":["128"]},
                    {"Name":"Last Check up","Values":["12\/20\/2018 04:00:08"]},
                    {"Name":"Last Notification","Values":["11\/22\/2018 05:00:59"]},
                    {"Name":"Last used by","Values":["David"]},
                    {"Name":"Last Used On","Values":["09\/11\/2018 14:57:50"]},
                    {"Name":"Validation","Values":["02\/22\/2019 04:06:18"]},
                    {"Name":"Last Validation query","Values":["Success"]},
                    {"Name":"Last Acceptence","Values":["04\/12\/2019 04:10:11"]},
                    {"Name":"Managed By","Values":["Manager"]},
                    {"Name":"Automatic","Values":["True"]},
                    {"Name":"Options","Values":["More options Needed"]},
                    {"Name":"Organization","Values":["Organization"]},
                    {"Name":"Organizational Unit","Values":["CSV"]},
                    {"Name":"Work Location Date","Values":["02\/29\/2019 02:00:40"]},
                    {"Name":"URL Details","Values":["CN=web.website.com,O=\"Organization\",OU=CSV,L=Toronto,ST=Ontario,C=CA"]},
                    {"Name":"Provence","Values":["Ontario"]},
                    {"Name":"Surname","Values":["Johnson"]},
                    {"Name":"Transaction value","Values":["23452523"]},
                    {"Name":"Validation Acceptance Values",
                                    "Values":["111.11.11.11|443|0|11|0|organization.com\\3$","111.11.111.111|443|0|11|0|orgaization.com\\2$","111.11.111.111|443|0|11|0|orgaization.com\\2$"]},
                    {"Name":"Acceptance  State","Values":["Success"]},
                    {"Name":"Validity Zone","Values":["120"]},
                    {"Name":"ClearMode","Values":["0"]},
                    {"Name":"Approval CA:Count","Values":["1"]},
                    {"Name":"Reader","Values":["Office"]},
                    {"Name":"End Date","Values":[""]},
                    {"Name":"Meeting Subject","Values":["adb.organization.com"]},
                    {"Name":"SubjectAltName DNS","Values":["adb.organization.com","ade.organization.com","Create.orgaization.com"]}],
"Result":1}

Solution

  • There are two problem lines in the JSON:

    • This line has a misplaced "

    {"Name":"Opened On","Values":["Mark, John, L=Toronto, S=Ontario, C=CA","www.doman.com", O=Office, L=Toronto, S=Ontario, C="CA"]},

    It should probably be

    {"Name":"Opened On","Values":["Mark, John, L=Toronto, S=Ontario, C=CA","www.doman.com", "O=Office, L=Toronto, S=Ontario, C=CA"]},

    • And this line

    {"Name":"Internet System Address","Values"["www.doman.com/Locations"]},

    should have a : after "Values"

    {"Name":"Internet System Address","Values":["www.doman.com/Locations"]},

    Once you've fixed those issues, the result is valid JSON and can be piped to ConvertFrom-JSON:

    $JSON = @"
    {"ResultForName":
        [{"Name":"work","Values":["no"]},
        {"Name":"location","Values":["\\pc\\documents"]},
        {"Name":"Authority","Values":["10"]},
        {"Name":"City","Values":["Toronto"]},
        {"Name":"Country","Values":["CA"]},
        {"Name":"Created By","Values":["George"]},
        {"Name":"Money Vault Id","Values":["100000"]},
        {"Name":"Opened  On","Values":["Mark, John, L=Toronto, S=Ontario, C=CA","www.doman.com", "O=Office, L=Toronto, S=Ontario, C=CA"]},
        {"Name":"Driver Name","Values":["Honda"]},
        {"Name":"Can drive","Values":["0"]},
        {"Name":"Given Name","Values":["Steven"]},
        {"Name":"Group Id","Values":["34324"]},
        {"Name":"Internet System Address","Values":["www.doman.com/Locations"]},
        {"Name":"Key value","Values":["AXU"]},
        {"Name":"Key Encryption","Values":["128"]},
        {"Name":"Last Check up","Values":["12\/20\/2018 04:00:08"]},
        {"Name":"Last Notification","Values":["11\/22\/2018 05:00:59"]},
        {"Name":"Last used by","Values":["David"]},
        {"Name":"Last Used On","Values":["09\/11\/2018 14:57:50"]},
        {"Name":"Validation","Values":["02\/22\/2019 04:06:18"]},
        {"Name":"Last Validation query","Values":["Success"]},
        {"Name":"Last Acceptence","Values":["04\/12\/2019 04:10:11"]},
        {"Name":"Managed By","Values":["Manager"]},
        {"Name":"Automatic","Values":["True"]},
        {"Name":"Options","Values":["More options Needed"]},
        {"Name":"Organization","Values":["Organization"]},
        {"Name":"Organizational Unit","Values":["CSV"]},
        {"Name":"Work Location Date","Values":["02\/29\/2019 02:00:40"]},
        {"Name":"URL Details","Values":["CN=web.website.com,O=\"Organization\",OU=CSV,L=Toronto,ST=Ontario,C=CA"]},
        {"Name":"Provence","Values":["Ontario"]},
        {"Name":"Surname","Values":["Johnson"]},
        {"Name":"Transaction value","Values":["23452523"]},
        {"Name":"Validation Acceptance Values",
                        "Values":["111.11.11.11|443|0|11|0|organization.com\\3$","111.11.111.111|443|0|11|0|orgaization.com\\2$","111.11.111.111|443|0|11|0|orgaization.com\\2$"]},
        {"Name":"Acceptance  State","Values":["Success"]},
        {"Name":"Validity Zone","Values":["120"]},
        {"Name":"ClearMode","Values":["0"]},
        {"Name":"Approval CA:Count","Values":["1"]},
        {"Name":"Reader","Values":["Office"]},
        {"Name":"End Date","Values":[""]},
        {"Name":"Meeting Subject","Values":["adb.organization.com"]},
        {"Name":"SubjectAltName DNS","Values":["adb.organization.com","ade.organization.com","Create.orgaization.com"]}],
    "Result":1}
    "@
    $JSON | ConvertFrom-Json