1

I have this file .json:

{
  "topologyTypes": {
    "stations": {
      "instances": [
        {
          "@name": "value1",
          "@address": "value2"
        },
        {
          "@name": "value3",
          "@address": "value4"
        }         
      ]
    }
  },
  "agg": {},
  "inter": {}
}

I want to add an object like this in topologyType.stations.instances with PowerShell

{
    "@name": "value4",
    "@adress": "value5"
}

So I tried this following code in PowerShell, but it doesn't work:

$path = "./data.json"
$jsonFile = Get-Content $path -Raw | ConvertFrom-Json

$jsonContent = @"
    {
        "@name": "value4",
        "@adress": "value5"
    }
"@

$jsonFile.topologyTypes.stations.instances |
    Add-Content -Value (ConvertFrom-Json $jsonContent)

The desired output I would like to get is like this:

{
  "topologyTypes": {
    "stations": {
      "instances": [
        {
          "@name": "value1",
          "@address": "value2"
        },
        {
          "@name": "value3",
          "@address": "value4"
        },
        {
          "@name": "value4",
          "@address": "value5"
        }         
      ]
    }
  },
  "agg": {},
  "inter": {}
}
5
  • 1
    "it doesn't work" is an insufficient problem description. How exactly does it "not work"? Are you getting an error? What is the expected and actual result? At first glance I can spot at least one PowerShell and one JSON syntax error. Commented Feb 3, 2018 at 14:59
  • Hi, thanks for the reply. I got this error : ConvertFrom-Json : invalid json primitive : [. (system.argunment.exception) + ... ations.instances | Add-Content -Value (ConvertFrom-Json $jsonContent) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand Commented Feb 3, 2018 at 15:09
  • 1
    Because the trailing comma in $jsonContent makes the JSON string invalid. But even if you remove it, you can't insert the new data into the JSON structure by appending to a file. Even the original JSON string you posted is invalid. Please edit your question show a minimal reproducible example of your code, the input, the desired output, and the arror that code gives you. Commented Feb 3, 2018 at 15:47
  • Ok so i modified my first post sorry.. Im not good with the syntax of json file Commented Feb 3, 2018 at 18:03
  • maybe just string replace [ with [ + $jsonContent before parsing it Commented Feb 3, 2018 at 18:12

1 Answer 1

6

Define the new content as a PowerShell custom object:

$jsonContent = [PSCustomObject]@{
    '@name'   = 'value4'
    '@adress' = 'value5'
}

append it to the instances substructure of your imported JSON data:

$jsonFile.topologyTypes.stations.instances += $jsonContent

then convert the data back to a JSON string:

$jsonFile | ConvertTo-Json -Depth 4

Note that ConvertTo-Json inserts a lot of intention space. If you want exactly the format you posted you need to do some pretty-printing yourself. Something like this might help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.