1

Hello following Problem: I have written a function that reads a json file, adds a new object to it an saves it back to json:

The Input File looks like this:

{"8":
{"p01":
    [{"date":"01.01.2021", "user":"p1"},{"date":"01.02.2021", "user":"p1"}]
}}

The function that adds a new key value pair to p1 table:

function Save($floor, $place, $date, $user){    
    $content=get-content("$psscriptroot\bookings.json")|convertfrom-json
    $hash=[pscustomobject]@{date="$date";user="$user"}
    $content.$floor.$place+= $hash
    $content.$floor.$place
    $write=$false
    while(!($write)){
        Try{
            #$content|ConvertTo-Json|out-file "$psscriptroot\bookings.json"
            $Write=$true
            
        } catch {
            $write= $false
        }
    }
}

Powershell output before conversion looks great:

Save 8 p01 "02.03.2021" "bla"

date user


01.01.2021 p1
01.02.2021 p1
02.03.2021 bla

But the json output of the function looks like this:

{
"8":  {
          "p01":  [
                      "@{date=01.01.2021; user=p1}",
                      "@{date=01.02.2021; user=p1}",
                      "@{date=02.03.2021; user=bla}"
                  ]
      }}

So i cannot access the objects under p01 anymore because the are not handled as objects. I want to get the exact output format as the input format Any clues what i am doing wrong here?

0 Answers0