Parent-Child Relationship with ID Values

Welcome Forums General PowerShell Q&A Parent-Child Relationship with ID Values

This topic contains 6 replies, has 6 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #151767

    Participant
    Topics: 1
    Replies: 1
    Points: -6
    Rank: Member

    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

  • #151772

    Moderator
    Topics: 8
    Replies: 878
    Points: 2,690
    Helping Hand
    Rank: Community Hero

    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 ?

  • #151775

    Participant
    Topics: 8
    Replies: 371
    Points: 423
    Helping Hand
    Rank: Contributor
    # 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
    
    
  • #151782
    js

    Participant
    Topics: 22
    Replies: 614
    Points: 1,263
    Helping Hand
    Rank: Community Hero

    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"
        }
    ]
    
  • #151790

    Moderator
    Topics: 2
    Replies: 50
    Points: 287
    Team MemberHelping Hand
    Rank: Contributor

    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

  • #152234

    Participant
    Topics: 1
    Replies: 1
    Points: -6
    Rank: Member

    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

  • #152853

    Participant
    Topics: 2
    Replies: 960
    Points: 1,744
    Helping Hand
    Rank: Community Hero

    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"
    

You must be logged in to reply to this topic.