locked
Parsing JSON with arrays RRS feed RRS feed

  • Question

  • Hello,

    please can you help me parsing json. It looks like:

    $Payload =
    "
    {
      "id": "123",
      "active": true,
      "identifier": 615,
      "first_name": "Den",
      "last_name": "Pasternak",
      "email": "Den.Pasternak@namecheap.com",
      "country": "UK",
      "city": "",
      "office_name": "",
      "on_business_trip": false,
      "trip_office_name": null,
      "contacts": [
        {
          "type": "gmail",
          "value": "dpasternak@gmail.com"
        },
        {
          "type": "phone",
          "value": "+440895212121"
        },
        {
          "type": "phone",
          "value": "+440895212121"
        }
      ],
      "secondary_positions": [
        {
          "position": "Director of IT",
          "position_profile_id": 126,
          "org_units": [
    
          ],
          "manager": {
            "id": 6,
            "first_name": "Daily",
            "last_name": "Mirror",
            "position": "Chief Operating Officer (COO)"
          },
          "rank_id": 10,
          "rank_name": "Director",
          "rank_index": 1,
          "role_id": 2,
          "role_name": "Leader",
          "role_index": 0
        }
      ],
      "position": "Director of IT",
      "position_profile_id": 126,
      "org_units": [
        {
          "id": 1,
          "name": "contoso.com",
          "type_id": 1,
          "type_name": "Company",
          "type_index": 0
        },
        {
          "id": 78,
          "name": "TECHNOLOGY",
          "type_id": 2,
          "type_name": "Business Module",
          "type_index": 1
        },
        {
          "id": 226,
          "name": "IT",
          "type_id": 3,
          "type_name": "Business Function",
          "type_index": 2
        }
      ],
      "manager": {
        "id": 4,
        "first_name": "Gaby",
        "last_name": "Hinsliff",
        "position": "Chief Technical Officer (CTO)"
      },
      "rank_id": 10,
      "rank_name": "Director",
      "rank_index": 1,
      "role_id": 2,
      "role_name": "Leader",
      "role_index": 0
    }
    "

    How do I pars it:

    $J = $payloadJson | ConvertFrom-Json
    
    
    $parsed.Clear()
    $parsed = @()
    foreach ($item1 in $J.psobject.Properties)
    {
      if  (($item1.TypeNameOfValue -eq "System.Object[]") -or ($item1.TypeNameOfValue -eq "System.Management.Automation.PSCustomObject"))
       {
            foreach ($level1 in $item1.Value)
            {
                foreach ($item2 in $level1.psobject.Properties)
                {
                    if (($item2.TypeNameOfValue -eq "System.Object[]") -or ($item2.TypeNameOfValue -eq "System.Management.Automation.PSCustomObject"))
                    {
                        foreach ($level3 in $item2.Value)
                        {
                            foreach ($item3 in $level2.psobject.Properties)
                            {
                                if (($item3.TypeNameOfValue -eq "System.Object[]") -or ($item3.TypeNameOfValue -eq "System.Management.Automation.PSCustomObject"))
                                {
                                    # next level
                                }           
                                else
                                {
                                    $obj = New-Object psobject -Property @{
                                            Name = "$($level1.Name).$($item2.Name).$($item3.name)"
                                            Value = $item3.Value            
                                            }
                                    $parsed += $obj
                                }
                            }
    
                        }
                    
                    }
                    else 
                    {
                       $obj = New-Object psobject -Property @{
                              Name = "$($item1.Name).$($item2.Name)"
                              Value = $item2.Value            
                              }
                       $parsed += $obj
                    }
                
                }
            }
       } 
       
       else
        {
          $obj = New-Object psobject -Property @{
          Name = $item1.Name
          Value = $item1.Value            
          }
          $parsed += $obj
        }
    }

    the result looks like:


    I think that the way I'm parsing json is not correct. Pleas tell me how to process json so that it would get all the values, including in arrays.

    Saturday, October 5, 2019 9:10 AM

Answers

All replies

  • convertfrom-json не работает?



    The opinion expressed by me is not an official position of Microsoft


    • Edited by Vector BCO Saturday, October 5, 2019 9:53 AM
    Saturday, October 5, 2019 9:22 AM
  • convertfrom-json не работает?



    The opinion expressed by me is not an official position of Microsoft


    It works, but I need to pars every line. In this case, I will need to register a variable for each line.

    I need all the lines to be automatically processed and added as a parameter / value for a new project. Further this object should be processed according to the conveyor.

    $payload can be changed (add/remove) values. I need automatically pars this changes.
    Saturday, October 5, 2019 12:54 PM
  • I would ask here:

    https://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by Guido Franzke Monday, November 4, 2019 2:29 PM
    • Marked as answer by Dave PatrickMVP Saturday, November 9, 2019 3:45 PM
    Monday, November 4, 2019 2:16 PM