1

I'm loading a text file containing some Json to edit a property. However, after modifying the content and writing it to file, the Json becomes invalid.I

I use the following PowerShell to modify the file:

$manifest = Get-Content $PathToManifest -Raw | ConvertFrom-Json

#modify Json    

Set-Content -Path $PathToManifest -Value ( $manifest | ConvertTo-Json)  

The following snippet from my Json file gets corrupted:

 "contributions": [
    {
      "id": "sample-data-widget",
      "type": "ms.vss-dashboards-web.widget",
      "targets": ["ms.vss-dashboards-web.widget-catalog"],
      "properties": "@{name=Sample Data; description=Create sample data in a VSTS project.; previewImageUrl=img/logo.png; uri=index.html; supportedSizes=System.Object[]; supportedScopes=System.Object[]}"
    }]

After loading the Json and writing it back to file the array syntax around targets is gone:

  "contributions": [
    {
      "id": "sample-data-widget",
      "type": "ms.vss-dashboards-web.widget",
      "targets": "ms.vss-dashboards-web.widget-catalog",
      "properties": "@{name=Sample Data; description=Create sample data in a VSTS project.; previewImageUrl=img/logo.png; uri=index.html; supportedSizes=System.Object[]; supportedScopes=System.Object[]}"
    }]

Why is this happening? Is there a way to make sure the syntax doesn't change?

5
  • Both of you json snippets look the same. Can you show what it looks like before? Commented Feb 18, 2016 at 15:55
  • ConvertTo-Json -Depth 999 ? Commented Feb 18, 2016 at 16:11
  • @Matt the top one is before, bottom one below. If you look at the targets property you see that the [ ] around the value are gone. Commented Feb 18, 2016 at 16:18
  • @beatcracker if you add that as answer, preferably with some explanation, I'll accept it! It works :) Commented Feb 18, 2016 at 16:24
  • I meant that I saw system.object[] on both. That is not part of your original I would imagine Commented Feb 18, 2016 at 18:08

1 Answer 1

5

ConvertTo-Json has Depth parameter that controls how many levels of contained objects are included in the JSON representation. The default value is 2. ConvertTo-Json will call .ToString() on anything nested deeper than specified Depth.

So all you need is to specify sufficiently large number for Depth argument or just ([int]::MaxValue).

Set-Content -Path $PathToManifest -Value ( $manifest | ConvertTo-Json -Depth ([int]::MaxValue))  

Examples of nesting and ConvertTo-Json behavior:

$NestedArray = @(1,@(2,@(3,@(4))))

Default:

$NestedArray | ConvertTo-Json

[
    1,
    {
        "value":  [
                      2,
                      [
                          3,
                          "4"
                      ]
                  ],
        "Count":  2
    }
]

No nesting at all:

$NestedArray | ConvertTo-Json -Depth 1

[
    1,
    {
        "value":  [
                      2,
                      "3 System.Object[]"
                  ],
        "Count":  2
    }
]

Desired result:

$NestedArray | ConvertTo-Json -Depth 3

[
    1,
    {
        "value":  [
                      2,
                      [
                          3,
                          [
                              4
                          ]
                      ]
                  ],
        "Count":  2
    }
]
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.