skip to Main Content

I know this type of questions has been asked but I cant find the exact answer to my specific question – if there is one please let me know and apologies…

I have two json files and need to merge/combine them so I end up with a final file

Example

File 1:

  {
        "ONE":  {
                    "A":  "value1",
                    "B":  "value2",
                    "C":  "value3"
                },
        "TWO":  {
                    "A":  "value4",
                    "B":  "value5",
                    "C":  "value6"
                }
    }

File 2:

    {
    "ONE":  {
                "D":  "valueY"
            },
    "TWO":  {
                "D":  "valueX"
            }
    }

Final File:

    {
    "ONE":  {
                "A":  "value1",
                "B":  "value2",
                "C":  "value3",
                "D":  "valueY"
            },
    "TWO":  {
                "A":  "value4",
                "B":  "value5",
                "C":  "value6",
                "D":  "valueX"
            }
    }

I can do a basic merge OK – but cant seem to figure out how to ‘insert’ or ‘add’ the "D" value into the first file so they are all in the final file?

Any advice would be great

Thanks

3

Answers


  1. You might use the Merge() method of the Newtonsoft.Json.Linq Namespace for this:

    # For older (Windows) PowerShell versions, you need to load the `newtonsoft.json.dll`
    Add-Type -Path <your path to>newtonsoft.json.dll
    
    # $Json1 = Get-Content -Raw .File1.Json
    $Json1 = '{"ONE":{"A":"value1","B":"value2","C":"value3"},"TWO":{"A":"value4","B":"value5","C":"value6"}}'
    # $Json2 = Get-Content -Raw .File2.Json
    $Json2 = '{"ONE":{"D":"valueY"},"TWO":{"D":"valueX"}}'
    
    $JObject1 = [Newtonsoft.Json.Linq.JObject]::Parse($Json1)
    $JObject2 = [Newtonsoft.Json.Linq.JObject]::Parse($Json2)
    $JObject1.Merge($JObject2)
    $JObject1.ToString()
    
    {
      "ONE": {
        "A": "value1",
        "B": "value2",
        "C": "value3",
        "D": "valueY"
      },
      "TWO": {
        "A": "value4",
        "B": "value5",
        "C": "value6",
        "D": "valueX"
      }
    }
    
    Login or Signup to reply.
  2. Here is a solution in Python3:

    
    A = {
        "ONE": {
            "A": "value1",
            "B": "value2",
            "C": "value3"
        },
        "TWO": {
            "A": "value4",
            "B": "value5",
            "C": "value6"
        }
    }
    
    B = {
        "ONE":  {
                    "D":  "valueY"
                },
        "TWO":  {
                    "D":  "valueX"
                }
        }
    
    final_dict = {}
    for item, value in A.items():
        for itm, val in B.items():
            final_dict[item] = {**value, **val}
    
    print(final_dict)
    
    
    {
       "ONE":{
          "A":"value1",
          "B":"value2",
          "C":"value3",
          "D":"valueX"
       },
       "TWO":{
          "A":"value4",
          "B":"value5",
          "C":"value6",
          "D":"valueX"
       }
    }
    
    
    Login or Signup to reply.
  3. You’ll want to convert these to objects that PowerShell can more easily manage.
    Not sure where the input is coming from – but you get the picture.

    $file1 = get-content -path file1_path
    $file1
      {
        "ONE":  {
                    "A":  "value1",
                    "B":  "value2",
                    "C":  "value3"
                },
        "TWO":  {
                    "A":  "value4",
                    "B":  "value5",
                    "C":  "value6"
                }
    }
    
    $file2 = get-content -path file2_path
    $file2
        {
        "ONE":  {
                    "D":  "valueY"
                },
        "TWO":  {
                    "D":  "valueX"
                }
        }
    

    Now turn them into hashtables

    $file1_hash = $file1 | convertfrom-json -ashashtable
    $file2_hash = $file2 | convertfrom-json -ashashtable
    

    As long as the names are the same "ONE, TWO" in your json files, then when you iterate through the 2nd file, the keys will align, and add the values:

    foreach ($h in $file2_hash.GetEnumerator()) {
        foreach ($kv in $h.Value) {
            $file1_hash.$($h.Key).$($kv.keys) = $($kv.values)
        }
    }
    

    fun times – now everything has been ‘merged’ into $file1_hash. So convert it back to json.

    $file1_hash | convertto-json
    

    OUTPUT:

    {
      "ONE": {
        "A": "Value1",
        "B": "Value2",
        "C": "Value3",
        "D": "ValueY"
      },
      "TWO": {
        "A": "Value4",
        "B": "Value5",
        "C": "Value6",
        "D": "ValueX"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search