0

I have a question about convert from/to json with powershell. I need a json with a specific format (for a REST Call) but if I do an convertfrom-json and convertto-json the format is wrong and I don't know how I can change it. This is the original format:

{
    "property-content":  [
                             {
                                 "statKey":  "Test|Test1",
                                 "timestamps":  [1522678260000],
                                 "values":  ["compliant"],
                                 "others":  "",
                                 "otherAttributes":  ""
                             }
                         ]
}

and if I do an converfrom-json ... then I change some values and then I do an convertto-json the format is without the brackets:

{
    "property-content":  [
                             {
                                 "statKey":  "Test|Test1",
                                 "timestamps":  "1522678260000",
                                 "values":  "compliant",
                                 "others":  "",
                                 "otherAttributes":  ""
                             }
                         ]
}

Thanks!

3
  • 1
    I don't do a lot of work with json, but I suspect that you need to force the new value to be an array before yoou change it in the json. Commented Apr 2, 2018 at 17:56
  • 1
    the square braces indicate the value is an array. if powershell doesn't interpret the input as an array then it won't output json text with the square braces. generally it doesn't matter, is your api complaining about it? Commented Apr 2, 2018 at 17:56
  • 1
    If you look a little deeper, you will notice that the single value array is not just changed to a value, but the value itself ."property-content"."timestamps" type has also been cast from an int64 to a string... Commented Apr 2, 2018 at 18:14

1 Answer 1

3

The arrays are flattened by the ConvertTo-Json cmdlet because of the -Depth parameter which is set to 2 by default. If you higher the -Depth it will resolve your issue:

PS C:\> $a = ConvertFrom-JSON @'
>> {
>>     "property-content":  [
>>                              {
>>                                  "statKey":  "Test|Test1",
>>                                  "timestamps":  [1522678260000],
>>                                  "values":  ["compliant"],
>>                                  "others":  "",
>>                                  "otherAttributes":  ""
>>                              }
>>                          ]
>> }
>> '@
PS C:\> $a | convertTo-JSON -Depth 5
{
    "property-content":  [
                             {
                                 "statKey":  "Test|Test1",
                                 "timestamps":  [
                                                    1522678260000
                                                ],
                                 "values":  [
                                                "compliant"
                                            ],
                                 "others":  "",
                                 "otherAttributes":  ""
                             }
                         ]
}
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.