PowerShell ConvertFrom-Json not converting JSON List to Json Object

Viewed 180

I have a json output as list, I wanted to add a property in tags parameter using PowerShell

$json = @'
[
    {
       "tags": [],
        "name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
        "properties": {
            "type": "AzureDataLakeStoreFile",
        }
    }
]
'@
#$json = 'C:\workspace\cucumber_report.29776.json'

$obj = $json | ConvertFrom-Json 

# Append new objects to the array.
$obj.tags += [pscustomobject] @{ name = 'newname1' }

# Convert back to JSON.
$obj | ConvertTo-Json -Depth 10 | Set-Content $json

But i am getting error as below The property 'tags' cannot be found on this object. Verify that the property exists and can be set. looks like issue is occurs only when I am passing JSON list, with normal json input this is working fine

1 Answers
  1. The trailing comma in line "type": "AzureDataLakeStoreFile", leads to an error ConvertFrom-Json : Invalid JSON primitive.
  2. $obj.GetType().Name returns Object[] (note that the most top element of the original json is an array []).

Use

$obj[0].tags += [pscustomobject] @{ name = 'newname1' }
Related