简体   繁体   中英

Combining Nested Json using PowerShell

I have the following Json script:

{
"merchant_info": {
    "email": "merchant@example.com",
    "first_name": "David",
    "last_name": "Larusso",
    "business_name": "Mitchell & Murray",
    "phone": {
        "country_code": "001",
        "national_number": "4085551234"
    },
    "address": {
        "line1": "1234 First Street",
        "city": "Anytown",
        "state": "CA",
        "postal_code": "98765",
        "country_code": "US"
    }
},
"billing_info": [{
        "email": "bill-me@example.com",
        "first_name": "Stephanie",
        "last_name": "Meyers"
    }
],
"shipping_info": {
    "first_name": "Stephanie",
    "last_name": "Meyers",
    "address": {
        "line1": "1234 Main Street",
        "city": "Anytown",
        "state": "CA",
        "postal_code": "98765",
        "country_code": "US"
    }
},
"items": [{
        "name": "Zoom System wireless headphones",
        "quantity": 2,
        "unit_price": {
            "currency": "USD",
            "value": "120"
        },
        "tax": {
            "name": "Tax",
            "percent": 8
        }
    }, {
        "name": "Bluetooth speaker",
        "quantity": 1,
        "unit_price": {
            "currency": "USD",
            "value": "145"
        },
        "tax": {
            "name": "Tax",
            "percent": 8
        }
    }
],
"discount": {
    "percent": 1
},
"shipping_cost": {
    "amount": {
        "currency": "USD",
        "value": "10"
    }
},
"note": "Thank you for your business.",
"terms": "No refunds after 30 days."
}

And I want to use PowerShell to get the following Record and export it to CSV:

期望的输出

So far I created the following Script:

$JsonFile = "C:\Users\me\Documents\myfile.json"

$OutputFile = "C:\Users\me\Documents\newtext.csv"

Get-Content -Path $OutputFile
$json = ConvertFrom-Json (Get-Content $JsonFile -Raw)
$json.merchant_info | Select "first_name","last_name",@{Label = "phone"; Expression = {$_.phone.national_number}} | 
Export-Csv $OutputFile -NoTypeInformation

I am able to bring values from (Merchant_info, Shipping_info, item) separetely but how do I bring it all in combined like in my screen shot above.

but how do I bring it all in combined like in my screen shot above.

We can only guess; assuming this entire json block is one order, with one merchant and one customer, but multiple items, then each row is an item. So start with that as the input:

Create an output record (PSCustomObject) with the repeated data, and then the individual item data:

$json.items | ForEach-Object {

    [PSCustomObject]@{
        MerchantInfoFirstName   = $json.merchant_info.first_name
        MerchantInfoLastName    = $json.merchant_info.last_name
        MerchantInfoPhoneNumber = $json.merchant_info.phone.national_number
        ShippingInfoFirstName   = $json.shipping_info.first_name
        ShippingInfoLastName    = $json.shipping_info.last_name

        ItemName = $_.name
        ItemQuantity = $_.quantity

    }

} | Export-Csv ... etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM