Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to remove Value and Count from Powershell JSON nested object

Tags:

powershell

In powershell I'm trying to convert CSV file to JSON with nested array of Size object by grouping products. In code below $ProductLines come from CSV file as powershell object.

 $UniqueProducts = $ProductLines | Group-Object -Property 'ProductNumber' | ForEach-Object {    

    $SizeObj = ($_.Group | Select-Object -Property `
      @{name="SizeCode"; Expression = {$_.Sizes}}, @{name="UPC"; Expression = {$_.UPCNo}} `
    )
    #$SizeObj | ConvertTo-Json -depth 10 | Out-File "C:\POWERSHELL\Sizes.txt"

    $_.Group | Select-Object -Property ProductNumber, ProductName, `
         @{name="Sizes"; Expression = {$SizeObj}} -Unique
  }

$UniqueProducts | ConvertTo-Json -depth 10 | Out-File "C:\POWERSHELL\UniqueProducts.txt"

The above code produces JSON file with below structure with additional layer value: and Count:

[
    {
        "ProductNumber":  "EBAGS101-00008",
        "ProductName":  "EBAGS101",
        "Sizes":  {
                      "value":  [
                                    {
                                        "SizeCode":  "XS",
                                        "UPC":  "201112291509"
                                    },
                                    {
                                        "SizeCode":  "S",
                                        "UPC":  "201112291510"
                                    }
                                ],
                      "Count":  2
                  }
    },
    {
        "ProductNumber":  "EBAGS101-001",
        "ProductName":  "EBAGS101",
        "Sizes":  {
                      "value":  [
                                    {
                                        "SizeCode":  "XS",
                                        "UPC":  ""
                                    },
                                    {
                                        "SizeCode":  "S",
                                        "UPC":  "098617106215"
                                    }
                                ],
                      "Count":  2
                  }
    }
]

How can I remove value and Count layer from my result. When I output just $SizeObj it doesn't have value and Count, I'm not sure how value and Count ends up in $UniquProducts. In my end result I'm trying to create SizeObj in the below format (without value and Count):

  "Sizes":   [
        {
            "SizeCode":  "XS",
            "UPC":  "201112291509"
        },
        {
            "SizeCode":  "S",
            "UPC":  "201112291510"
        }
    ]

UPDATE: using answer provided by @RoadRunner here is the modified solution with csv source; where $ProductLines is converted Powershell object from csv file:

$Products = New-Object -TypeName System.Collections.ArrayList
$ProductLines | Group-Object -Property 'ProductNumber' | ForEach-Object {
  $SizeRanges = New-Object -TypeName System.Collections.ArrayList
  foreach ($object in $_.Group) {
    $SizeVal = [PSCustomObject]@($object | Select-Object -Property `
        @{name="SizeCode"; Expression = {$_.Sizes}}, `
        @{name="UPC"; Expression = {$_.UPCNo}}`
      )
      $SizeRanges.AddRange($SizeVal)|Out-Null
  }
  $grp = $_.Group[0];
  $product =  [PSCustomObject]@{
      ProductNumber = $grp.ProductNumber
      ProductName = $grp.ProductName
      Sizes = $SizeRanges
  }
  $products.Add($product)|Out-Null
} | ConvertTo-Json -Depth 3 | Out-File -FilePath output.json
like image 299
Richard Mneyan Avatar asked Oct 28 '25 05:10

Richard Mneyan


1 Answers

I would just rebuild the JSON with the properties you want to keep.

First you can get the JSON data with Get-Content and ConvertFrom-Json. Then you can iterate each JSON object, creating a new PSCustomObject which keeps the ProductNumber, ProductName and Sizes properties, where the Sizes is just the array from Sizes.value. We can then convert to a JSON structure with ConvertTo-Json, keeping the first three levels with -Depth 3, and export the result to an output file with Out-File.

$json = Get-Content -Path .\data.json | ConvertFrom-Json

& {
    foreach ($object in $json)
    {
        [PSCustomObject]@{
            ProductNumber = $object.ProductNumber
            ProductName = $object.ProductName
            Sizes = $object.Sizes.value
        }
    }
} | ConvertTo-Json -Depth 3 | Out-File -FilePath output.json

You could probably also integrate the above logic in what you already have, so you don't need to rebuild the JSON in the first place. Unfortunately I can't really show how to do that because all I can see is the JSON you are producing, so I can only recommend a solution to rebuild the JSON to your requirements.

output.json

[
  {
    "ProductNumber": "EBAGS101-00008",
    "ProductName": "EBAGS101",
    "Sizes": [
      {
        "SizeCode": "XS",
        "UPC": "201112291509"
      },
      {
        "SizeCode": "S",
        "UPC": "201112291510"
      }
    ]
  },
  {
    "ProductNumber": "EBAGS101-001",
    "ProductName": "EBAGS101",
    "Sizes": [
      {
        "SizeCode": "XS",
        "UPC": ""
      },
      {
        "SizeCode": "S",
        "UPC": "098617106215"
      }
    ]
  } 
]
like image 97
RoadRunner Avatar answered Oct 30 '25 05:10

RoadRunner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!