Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

How to add a value and key into existing JSON file over powershell?

I would like to add an additional key with value into my existing JSON file. Unfortunately I'm not able. Here an short overview:

My JSON-File before powershell script is run:

[
  {
    "id": "1",
    "description": [
      {
        "country": "Brazil"
      },
      {
        "country": "Mexico"
      }
    ]
  },
  {
    "id": "2",
    "description": [
      {
        "country": "Argentina"
      }
    ]
  }
]

My wish, how the JSON-File should look like, after my powershell script is run:

[
  {
    "id": "1",
    "description": [
      {
        "country": "Brazil",
        "city": "Rio de Janeiro"
      },
      {
        "country": "Mexico",
        "city": "Mexico City"
      }
    ]
  },
  {
    "id": "2",
    "description": [
      {
        "country": "Argentina",
        "city": "Buenos Aires"
      }
    ]
  }
]

My powershell script:

function GetCity($country) {
    $x = "not available"                
    If ( $country -eq "Brazil" ) { $x = "Rio de Janeiro" }
    If ( $country -eq "Mexico" ) { $x = "Mexico City" }
    If ( $country -eq "Argentina" ) { $x = "Buenos Aires" }    
    return $x    
}

# Source the JSON content
$jsonFile = 'C:Tempest.json'
$jsonContent  = Get-Content -Path $jsonFile

# Convert JSON to PSObjects
$jsonAsPsObjects = $jsonContent | ConvertFrom-Json

foreach ($info in $jsonAsPsObjects) {
    $result = GetCity($info.description.country)
    jsonContent | Add-Member -Type NoteProperty -Name "City" -Value $result
}

# Save JSON back to file
$json | ConvertTo-Json | Set-Content $jsonFile

Error:

jsonContent : The term 'jsonContent' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

How can I solve this issue?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There at two problems:

  • jsonContent should be $jsonContent in statement jsonContent | Add-Member ...

  • You're neglecting to loop over the array elements of the description property, to each of which a city property is to be added.

I suggest streamlining your code as follows:

function Get-City {
  param([string] $country)
  # Use a `switch` statement:
  switch ($country) {
    'Brazil' { return 'Rio de Janeiro' }
    'Mexico' { return 'Mexico City' }
    'Argentina' { return 'Buenos Aires' }
    default { return 'not available' }
  }
}

$jsonFile = 'C:Tempest.json'

(Get-Content -Raw $jsonFile | ConvertFrom-Json) | ForEach-Object {
  # Add a 'city' property to each object in the 'description' property.
  $_.description.ForEach({ 
    Add-Member -InputObject $_ city (Get-City $_.country) 
  })
  $_  # output the modified object
} | ConvertTo-Json -Depth 3  # | Set-Content $jsonFile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...