Parent-Child Relationship with ID Values

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

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

 
Participant
8 hours, 5 minutes ago.

  • Author
    Posts
  • #151767

    Participant
    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
    Points: 1,959
    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
    Points: 315
    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
    Points: 948
    Helping Hand
    Rank: Major Contributor

    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
    Points: 260
    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
    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

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort