I need to load a JSON file into powershell, and then process the various nodes from the JSon file. The file is loaded from an existing Json file:
$JSONContent = (Get-Content -Path "C:\JsonFile.json" -Raw) | ConvertFrom-Json
Once this is loaded, I can see the data values by:
$JSONContent | SELECT -expand Results
which then gives me a list format of the fields and values.
My problem is that I want to iterate through the $JSONContent
object and for each logical record (delimited by comma), concatenate each field value to make a comma-separated string.
I am sure the answer is trivial, but I have tried to resolve this. If any kind person could help with this, then I would be grateful.
The JSON file as input is:
{
"results": [
{
"id": 1,
"id2": null,
"first_name": "Jeanette",
"last_name": "Penddreth",
"email": "jpenddreth0@census.gov",
"gender": "Female",
"ip_address": "26.58.193.2",
"serverlist": [
{
"myval1": "testdata1",
"myval2": "testdata2",
"myval3": "testdata3"
}
],
"aitlist": [
{
"ait1": "aitdata1",
"ait2": "aitdata2"
}
]
},
{
"id": 2,
"first_name": "Giavani",
"last_name": "Frediani",
"email": "gfrediani1@senate.gov",
"gender": "Male",
"ip_address": "229.179.4.212",
"serverlist": [
{
"myval1": "testdata5",
"myval2": "testdata6",
"myval3": "testdata7"
},
{
"myval1": "testdata9",
"myval2": "testdata10",
"myval3": "testdata11"
}
],
"aitlist": [
{
"ait1": "aitdata3",
"ait2": "aitdata4"
}
]
},
{
"id": 3,
"first_name": "Noell",
"last_name": "Bea",
"email": "nbea2@imageshack.us",
"gender": "Female",
"ip_address": "180.66.162.255"
},
{
"id": 4,
"first_name": "Willard",
"last_name": "Valek",
"email": "wvalek3@vk.com",
"gender": "Male",
"ip_address": "67.76.188.26"
}
]
}
The objective is to end up with the values from each node as a comma-separated string like this, using the example of the first record:
1,"Jeanette","Penddreth","jpenddreth0@census.gov","Female", "26.58.193.2","testdata1","testdata2","testdata3","aitdata1","aitdata2"