Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with converting json to csv

Status
Not open for further replies.

meetme

Programmer
Oct 15, 2001
8
0
0
US
Hello all,

I am trying to convert a json file to csv using Powershell. I created the script but it throws an error "Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'."

The json is in the below format

{
"STARTEND" : "S",
"ITEMS" :
[
{
"ItemID" : "00501016204234",
"Locked" : "N",
"Quantity" : 12,
"Delimiters" :
[
{
"PurchaseOrderID" : "00000000000000000001",
"Quantity" : "12",
"COOM" : "IN",
"COOP" : "IN",
"COOF" : "IN"
}
]
},
{
"ItemID" : "00501016503229",
"Locked" : "N",
"Quantity" : 5,
"Delimiters" :
[
{
"PurchaseOrderID" : "00000000000000000001",
"Quantity" : "5",
"COOM" : "IN",
"COOP" : "IN",
"COOF" : "IN"
}
]
}
]
}

My script is as below

$pathToJsonFile = "E:\DataLoad\inventory_replica.json"
$pathToOutputFile = "E:\DataLoad\inventory_replica.csv"
((Get-Content -Path $pathToJsonFile) | ConvertFrom-Json) | ForEach-Object {
$STARTEND = $_.Name
$ITEMS = $_.ITEMS | ForEach-Object {
$ItemID = $_.ItemID
$Locked = $_.Locked
$Quantity = $_.Quantity
$Delimiters += $_.Delimiters | ForEach-Object {
[pscustomobject] @{
'STARTEND' = $STARTEND
'ITEMS' = $ITEMS
'ItemID' = $ItemID
'Locked' = $Locked
'PurchaseOrderID' = $_.PurchaseOrderID
'Quantity' = $_.Quantity
'COOM' = $_.COOM
'COOP' = $_.COOP
'COOF' = $_.COOF
}
}
}
}
$Delimiters | Export-CSV $pathToOutputFile -NoTypeInformation

Thanks,

Seth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top