skip to Main Content

I have two json files, and I want to combine one json file as object to another json file.

My first C1 json like:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "connections_TestValue_name": {
            "defaultValue": "TestValue",
            "type": "String"
        }
    },
    "variables": {},
    "resources": []
}

And another C2 json like:

{
    "kind":  "V2",
    "properties":  {
                       "displayName":  "XXXX.XXX",
                       "authenticatedUser":  {
                                                 "name":  "XXX.XXX"
                                             },
                       "overallStatus":  "Connected",
                       "statuses":  [
                                        {
                                            "status":  "Connected"
                                        }
                                    ],
                       "connectionState":  "Enabled",
                       "parameterValues":  {

                                           },
                       "customParameterValues":  {

     ...
     ...
}

Now, I need to insert Json file C2 as object "resources": [] to another json file C1, like:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "connections_TestValue_name": {
            "defaultValue": "TestValue",
            "type": "String"
        }
    },
    "variables": {},
    "resources": [
      {
       "kind":  "V2",
       "properties":  {
        ...
        ...
      }
  ]
}

Note: C1, C2 are not actual files, they are obtained by other scripts.

2

Answers


  1. Chosen as BEST ANSWER

    Since I'm not an expert in powershell, I found the answer after many tries and debugging:

    $C1= $C1 | ConvertFrom-Json
    $C2= $C2 | ConvertFrom-Json
    
    $C1.resources+= $C2
    
    $C1New= $C1 | ConvertTo-Json -depth 100
    
    Write-Host "$C1New"
    

  2. Direction:

    $C1Data = ConvertFrom-Json $C1 -AsHashTable
    $C2Data = ConvertFrom-Json $C2 -AsHashTable
    $C1Data.resources = $C2Data
    $C1Data |ConvertTo-Json -Depth 9
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search