PowerShell script to convert Complex Nested JSON to CSV file format

Recently, I was working on a small utility to convert JSON data to CSV format. In this post, I will be sharing how we can use a simple PowerShell script for converting Complex Nested JSON to CSV format.

Refer for PowerShell script to convert CSV file format to Complex Nested JSON

Step 1: Sample Input JSON

[
    {
        "Name":  "Hyundai",
        "Desc":  "Hyundai Sample Desc",
        "Models":  [
                       {
                           "Name":  "Hyundai Creta",
                           "Desc":  "Hyundai Creta Sample Desc",
                           "BodyType":  "SUV",
                           "Rating":  4.5,
                           "Variants":  [
                                            {
                                                "Name":  "Hyundai Creta SX Executive",
                                                "Desc":  "Hyundai Creta SX Executive Sample Desc",
                                                "Price":  13.34,
                                                "Transmission":  "Manual",
                                                "Fuel":  "Petrol"
                                            },
                                            {
                                                "Name":  "Hyundai Creta SX Opt Diesel AT",
                                                "Desc":  "Hyundai Creta SX Opt Diesel AT Sample Desc",
                                                "Price":  15.16,
                                                "Transmission":  "Automatic",
                                                "Fuel":  "Diesel"
                                            }
                                        ]
                       },
                       {
                           "Name":  "Hyundai Verna",
                           "Desc":  "Hyundai Verna Sample Desc",
                           "BodyType":  "Sedan",
                           "Rating":  4.3,
                           "Variants":  [
                                            {
                                                "Name":  "Hyundai Verna S Plus",
                                                "Desc":  "Hyundai Verna S Plus Sample Desc",
                                                "Price":  9.69,
                                                "Transmission":  "Manual",
                                                "Fuel":  "Petrol"
                                            },
                                            {
                                                "Name":  "Hyundai Verna S Plus Diesel",
                                                "Desc":  "Hyundai Verna S Plus Diesel Sample Desc",
                                                "Price":  10.88,
                                                "Transmission":  "Manual",
                                                "Fuel":  "Diesel"
                                            }
                                        ]
                       }
                   ]
    },
    {
        "Name":  "Kia",
        "Desc":  "Kia Sample Desc",
        "Models":  [
                       {
                           "Name":  "Kia Seltos",
                           "Desc":  "Kia Seltos Sample Desc",
                           "BodyType":  "SUV",
                           "Rating":  4.4,
                           "Variants":  [
                                            {
                                                "Name":  "Kia Seltos HTE D",
                                                "Desc":  "Kia Seltos HTE D Sample Desc",
                                                "Price":  10.45,
                                                "Transmission":  "Manual",
                                                "Fuel":  "Diesel"
                                            }
                                        ]
                       }
                   ]
    }
]

Step 2: PowerShell script to convert Complex Nested JSON to CSV Data

JsonToCsv.ps1 PowerShell script content:

$pathToJsonFile = "D:\CSV to Json Sample\SampleDataCarInfo.json"
$pathToOutputFile = "D:\CSV to Json Sample\SampleDataCarInfoOutput.csv"
((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json) | ForEach-Object {
$Make = $_.Name
$MakeDescription = $_.Desc
$Models = $_.Models | ForEach-Object {
    $Model = $_.Name
    $ModelDescription = $_.Desc
    $ModelBodyType = $_.BodyType
    $ModelRating = $_.Rating
    $Variants += $_.Variants | ForEach-Object {
       [pscustomobject] @{
            'Make' = $Make
            'MakeDescription' = $MakeDescription
            'Model' = $Model
            'ModelDescription' = $ModelDescription
            'ModelBodyType' = $ModelBodyType
            'ModelRating' = $ModelRating
            'Variant' = $_.Name
            'VariantDescription' = $_.Desc
            'Price' = $_.Price
            'Transmission' = $_.Transmission
            'Fuel' = $_.Fuel
        }
    }
}
} 

$Variants | Export-CSV $pathToOutputFile -NoTypeInformation

Here:

$pathToJsonFile = “D:\CSV to Json Sample\SampleDataCarInfo.json”
$pathToOutputFile = “D:\CSV to Json Sample\SampleDataCarInfoOutput.csv”

Step 3: Execute PowerShell script to convert Complex Nested JSON to CSV Data

Json To Csv

SampleDataCarInfoOutput.csv 

CSV output

That’s It !!!

You may also like...

2 Responses

  1. karelyn says:

    {
    “$type”: “dbo.Soa.Core.DataContracts.PagedResult`1[[dbo.Soa.Core.DataContracts.GenericEntityData, Soa.Contracts]], Soa.Contracts”,
    “Items”: {
    “$type”: “System.Collections.Generic.List`1[[dbo.Soa.Core.DataContracts.GenericEntityData, Soa.Contracts]], mscorlib”,
    “$values”: [
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericEntityData, Soa.Contracts”,
    “EntityTypeName”: “Maker”,
    “PrimaryParentEntityTypeName”: “Party”,
    “Identity”: {
    “$type”: “dbo.Soa.Core.DataContracts.IdentityData, Soa.Contracts”,
    “EntityTypeName”: “Maker”,
    “IdentityElements”: {
    “$type”: “System.Collections.ObjectModel.Collection`1[[System.String, mscorlib]], mscorlib”,
    “$values”: [
    “2020”,
    “17847”
    ]
    }
    },
    “PrimaryParentIdentity”: {
    “$type”: “dbo.Soa.Core.DataContracts.IdentityData, Soa.Contracts”,
    “EntityTypeName”: “Party”,
    “IdentityElements”: {
    “$type”: “System.Collections.ObjectModel.Collection`1[[System.String, mscorlib]], mscorlib”,
    “$values”: [
    “2020”
    ]
    }
    },
    “Properties”: {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyDataCollection, Soa.Contracts”,
    “$values”: [
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “ID”,
    “Value”: “2020”
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “SEQN”,
    “Value”: {
    “$type”: “System.Int32”,
    “$value”: 17847
    }
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “FirstName”,
    “Value”: “Karen”
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “LastName”,
    “Value”: “K ”
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “Company”,
    “Value”: “Q Pte Ltd”
    }

    ]
    }
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericEntityData, Soa.Contracts”,
    “EntityTypeName”: “Events”,
    “PrimaryParentEntityTypeName”: “Party”,
    “Identity”: {
    “$type”: “dbo.Soa.Core.DataContracts.IdentityData, Soa.Contracts”,
    “EntityTypeName”: “Events”,
    “IdentityElements”: {
    “$type”: “System.Collections.ObjectModel.Collection`1[[System.String, mscorlib]], mscorlib”,
    “$values”: [
    “2020”,
    “17848”
    ]
    }
    },
    “PrimaryParentIdentity”: {
    “$type”: “dbo.Soa.Core.DataContracts.IdentityData, Soa.Contracts”,
    “EntityTypeName”: “Party”,
    “IdentityElements”: {
    “$type”: “System.Collections.ObjectModel.Collection`1[[System.String, mscorlib]], mscorlib”,
    “$values”: [
    “2020”
    ]
    }
    },
    “Properties”: {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyDataCollection, Soa.Contracts”,
    “$values”: [
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “ID”,
    “Value”: “2020”
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “SEQN”,
    “Value”: {
    “$type”: “System.Int32”,
    “$value”: 17847
    }
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “FirstName”,
    “Value”: “Karen”
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “LastName”,
    “Value”: “K ”
    },
    {
    “$type”: “dbo.Soa.Core.DataContracts.GenericPropertyData, Soa.Contracts”,
    “Name”: “Company”,
    “Value”: “Q Pte Ltd”
    }

    ]
    }

    }
    ]
    },
    “Offset”: 0,
    “Limit”: 100,
    “Count”: 100,
    “TotalCount”: 162,
    “NextPageLink”: null,
    “HasNext”: true,
    “NextOffset”: 100
    }

    How do I convert this to csv , i followed your steps but I’m lost.

  1. August 24, 2021

    […] Refer for PowerShell script to convert Complex Nested JSON to CSV file format […]

Leave a Reply

Your email address will not be published. Required fields are marked *