0

I have a json File... Here is an excerpt:

{
    "stuff1": "thisStuff1",
    "stuff2": "this Stuff2",
    "manifest": {
        "junk1": "This Junk 1",
        "build-version": "2.0.15299",
        "junk2": "This Junk 2",      
}

I can't change or modify the json file. I need the Version number in "build-Version"

I am writing a Powershell script to find the current version out of the json file.

I load in the Json file like this:

$json = (Get-Content "myPath.myJson.json" -Raw) | ConvertFrom-Json

I can get the Manifest like this:

$version = $json[0].manifest[0]
Write-Host $version

What I need is the Build Number, but since the Json file was built with dashes to connect names I get an error when I try:

$version = $json[0].manifest[0].build-version
Write-Host "The  Version" $version

enter image description here

This question seems close: How to deserialize a property with a dash (“-”) in it's name with NewtonSoft JsonConvert?

but I do not know the Powershell commands to deserialize

AWoods
  • 98
  • 8
  • 3
    the hyphen breaks parsing, use quotes around it ;) `$json[0].manifest[0].'build-version'` – Santiago Squarzon Feb 22 '23 at 22:12
  • 1
    Use `'...'` around property names that contain special characters, e.g. `$object.'foo-bar'`, as shown in the linked duplicate. As for what characters PowerShell considers special in identifiers, see [this answer](https://stackoverflow.com/a/65268127/45375). – mklement0 Feb 22 '23 at 23:42
  • 1
    @zett42, while `.{...}` will _typically_ work, I suggest avoiding it, because it involves unnecessary construction of a _script block_, which can - at least hypothetically - _break_, if the name amounts to something that isn't valid _PowerShell syntax_, such as `$obj.{foo < bar}` – mklement0 Feb 22 '23 at 23:46
  • 1
    @mklement0 Thanks for clarifying, I think I confused the syntax with `${…}`. Going to delete these comments as they don't really help. – zett42 Feb 23 '23 at 00:59

0 Answers0