# Parent-Child Relationship with ID Values

**URL:** <https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398>\
**Category:** PowerShell Help\
**Created:** [April 15, 2019, 7:18am UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398 "2019-04-15T07:18:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tb-killa](https://avatars.discourse-cdn.com/v4/letter/t/9de0a6/32.png) [@tb-killa](https://forums.powershell.org/u/tb-killa)\
**Post date:** [April 15, 2019, 7:18am UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398/1 "2019-04-15T07:18:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 15, 2019, 7:54am UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398/2 "2019-04-15T07:54:56Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [April 15, 2019, 8:01am UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398/3 "2019-04-15T08:01:13Z")

</div>

```
# 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
```

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [April 15, 2019, 9:00am UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398/4 "2019-04-15T09:00:04Z")

</div>

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"
    }
]
```

---

<div class="post-metadata">

**Author:** ![wes-stahler](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wes-stahler/32/227_2.png) [@wes-stahler](https://forums.powershell.org/u/wes-stahler)\
**Post date:** [April 15, 2019, 9:27am UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398/5 "2019-04-15T09:27:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tb-killa](https://avatars.discourse-cdn.com/v4/letter/t/9de0a6/32.png) [@tb-killa](https://forums.powershell.org/u/tb-killa)\
**Post date:** [April 18, 2019, 1:31am UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398/6 "2019-04-18T01:31:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [April 21, 2019, 11:38pm UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398/7 "2019-04-21T23:38:51Z")

</div>

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"
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/parent-child-relationship-with-id-values/12398/8 "2024-05-16T20:32:55Z")

</div>


