Good afternoon, everyone,
I’m facing a stupid problem and I just can’t think of a real solution.
Maybe someone can help me ?
I have a set of data (Sample) in the following format:
{"CID":"145","Name":"XX-2","ParentID":"0"},
{"CID":"2","Name":"XX-1","ParentID":"1"},
{"CID":"9","Name":"XX-0","ParentID":"8"},
The ParentID starts with “Zero”, so Zero is always the top parent relationship.
Below that the ParentID can be = CID to represent another Parent relationship.
I need an output in a format of my choice, I like to use convertTo-Json where I can list a parent-to-child relationship.
I’m hoping for your help.
A thousand thanks.
Greetings
Above content doens not suffice the requirment to be a valid json. but you can treat each line as a json content the convert it. Do you have any code which you have attempted to acheive your output ?
# Input is not recognized as JSON, so parsing it manually
$myInput = '
{"CID":"145","Name":"XX-2","ParentID":"0"}
{"CID":"2","Name":"XX-1","ParentID":"1"}
{"CID":"9","Name":"XX-0","ParentID":"8"}
'
# Parse input
$RawRecords = ($myInput.Split('}')).Trim().Replace('{','') | where { $_ }
' '
"Raw records:"
$RawRecords
$ParsedRecords = $RawRecords | foreach {
$Temp = $_.Split(',').Replace('"','')
[PSCustomObject]@{
$Temp[0].Split(':')[0] = $Temp[0].Split(':')[1]
$Temp[1].Split(':')[0] = $Temp[1].Split(':')[1]
$Temp[2].Split(':')[0] = $Temp[2].Split(':')[1]
}
}
' '
"Parsed records:"
$ParsedRecords
# Identify Root
$myRoot = $ParsedRecords.ParentID | sort | select -First 1 # Root ID
$myRoot = $ParsedRecords | where ParentID -EQ $myRoot # Root is the lowest ID parent
' '
"Identified Root (record with lowest parent ID):"
$myRoot
js2010
April 15, 2019, 9:00am
4
That could be json if it was surrounded by square brackets:
PS C:\> [pscustomobject]@{CID='145';Name='XX=2';ParentID='0'},[pscustomobject]@{CID='2';Name='XX=1';ParentID='1'},[pscustomobject]@{CID=
'9';Name='XX=0';ParentID='8'} | convertto-json
[
{
"CID": "145",
"Name": "XX=2",
"ParentID": "0"
},
{
"CID": "2",
"Name": "XX=1",
"ParentID": "1"
},
{
"CID": "9",
"Name": "XX=0",
"ParentID": "8"
}
]
Similar to JS, add the “ ” and you are good to go.
$array = ‘[
{“CID”:“145”,“Name”:“XX-2”,“ParentID”:“0”},
{“CID”:“2”,“Name”:“XX-1”,“ParentID”:“1”},
{“CID”:“9”,“Name”:“XX-0”,“ParentID”:“8”}
]’ | ConvertFrom-Json
$array | Out-GridView
Good afternoon, everyone.
Thank you very much for the input.
I think I have expressed myself wrongly with my question.
I apologize for this as a matter of urgency.
In advance… The data as input comes out in JSON format, this is no problem.
What I need is some kind of Parent to Child setup.
By this I mean, I have the ParentID and listed below the Childs.
Sort of like that:
Name":"XX-2" (Parent)
- Name":"XX-2.1" (Child)
- Name":"XX-2.2" (Child)
The ParentID starts with “Zero”, so Zero is always the top parent relationship.
Below that the ParentID can be = CID to represent another Parent relationship.
I hope I have now asked a more understandable question.
Thank you again for the effort and support.
Greetings
You mean this…
('[
{"CID":"145","Name":"XX-2","ParentID":"0"},
{"CID":"145","Name":"XX-2.1","ParentID":"0"},
{"CID":"145","Name":"XX-2.2","ParentID":"0"},
{"CID":"2","Name":"XX-1","ParentID":"1"},
{"CID":"9","Name":"XX-0","ParentID":"8"}
]' |
ConvertFrom-Json) |
Sort-Object -Property ParentID, Name |
Select Name, ParentID, CID
# Results
Name ParentID CID
---- -------- ---
XX-2 0 145
XX-2.1 0 145
XX-2.2 0 145
XX-1 1 2
XX-0 8 9
# Or this...
Clear-Host
(
$Data = @'
{"CID":"145","Name":"XX-2","ParentID":"0"},
{"CID":"145","Name":"XX-2.1","ParentID":"0"},
{"CID":"145","Name":"XX-2.2","ParentID":"0"},
{"CID":"2","Name":"XX-1","ParentID":"1"},
{"CID":"9","Name":"XX-0","ParentID":"8"}
'@ -replace '{|}'
) |
ConvertFrom-Csv -Header CID,Name,ParentID |
Sort-Object -Property ParentID, Name |
Select Name, ParentID, CID
# Results
Name ParentID CID
---- -------- ---
Name:"XX-2" ParentID:"0" CID:"145"
Name:"XX-2.1" ParentID:"0" CID:"145"
Name:"XX-2.2" ParentID:"0" CID:"145"
Name:"XX-1" ParentID:"1" CID:"2"
Name:"XX-0" ParentID:"8" CID:"9"