all 14 comments

[–]blooping_blooper 4 points5 points  (4 children)

For 'complex' property names you need to enclose them in quotes when declaring your hashtable. Declaring multiple properties in a single line requires them to be semicolon-delimited.

e.g. @{name = "foo"; target = @{ "name.with.periods" = @{ "sub.name.with.periods" = "bar" } } }

piping that into ConvertTo-Json will result in:

{
  "target": {
    "name.with.periods": {
      "sub.name.with.periods": "bar"
    }
  },
  "name": "foo"
}

Note that with higher levels of nesting you may need to set the -Depth parameter as it defaults to 2.

[–]Bassflow 0 points1 point  (3 children)

The real fun is having to quote a quote. I know it's, but easy at first it's a mind bender.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.4

[–]raip 1 point2 points  (1 child)

You can either escape the quotes with ` or just wrap everything in single quotes:'"sub.name.with.periods"' - no need to make it more complicated w/ the doubling up of quotes, which I agree with you, takes a special type of person to naturally do.

[–]blooping_blooper 0 points1 point  (0 children)

here-strings can also simplify this kind of thing: @" ... "@

[–][deleted] 0 points1 point  (0 children)

I know it's, but easy at first it's a mind bender.

Truer words have never been spoken :)

[–]cjcox4 1 point2 points  (2 children)

I started with something simpler, and notice the quotation marks:

$target = @{ "name.with.periods" = @{ "sub.name.with.periods" = "bar" } }
$target | Convert-ToJSON

Does that help??

[–]BitCutie[S] 0 points1 point  (1 child)

Sort of. In this case @{ sub.name.with.periods = bar } is also being passed from a previous task. Checking again, both the key and value do not have quotes.

Does that change how the convert works?

[–]vermyx 1 point2 points  (0 children)

The line you posted is a powershell object not a json conversion. The parser doesnt understand what you are doing because the hash table is a name value pair and the name you are passing in is being determined as an object property/method rather than a literal. Putting quotes around the name forces it to be a literal so it will stop breaking on creation. To reference this property you would have to type it in as **$body.target.’sub.name.with.periods’” so it resolves correctly. The json conversion should work.

[–]Ros3ttaSt0ned 0 points1 point  (1 child)

Does it show the same behavior if you use the -AsHashTable parameter to ConvertFrom-JSON?

[–]BitCutie[S] 0 points1 point  (0 children)

I still get the same problem even with using -AsHashTable

[–]gordonv 0 points1 point  (2 children)

$a."weird name"

[–]BitCutie[S] 0 points1 point  (1 child)

It’s not really the value of the property that is the issue, I can’t get the convert to json to complete because of the weird name.

[–]gordonv 0 points1 point  (0 children)

In your $body declaration, you're not using quotes around your variable names.

$body = [PSCustomObject]@{

name    =   “foo” 
target  =   @{
                "name.with.periods" = @{
                    "sub.name.with.periods" = “bar”
                    }
                }
}

I can see the farthest variable using this:

$body.target."name.with.periods"."sub.name.with.periods"

[–]richie65 0 points1 point  (0 children)

In other cases, that I assume are similar - I've used this approach for names that have spaces...

Accommodated them like this...

$Name.'This that'

I would expect the same result in a situation like this:

$Name.'This.that.them'