none
Parsing JSON with arrays RRS feed RRS feed

  • Вопрос

  • 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.

    5 октября 2019 г. 9:10

Все ответы

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



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


    5 октября 2019 г. 9:22
    Модератор
  • 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.
    5 октября 2019 г. 12:54