简体   繁体   中英

PowerShell- Merging JSON files

How to copy the objects of one array of a JSON file to an array of another JSON file using PowerShell? For Example I have one JSON file like:

"type":  "Employee",
"Properties":  [
                  {
                      "Name":  "Raj",
                      "Id":  "18111",
                      "email":  "emp1@company.com",
                      "Position":  "Manager",
                      "DateOfJoining":  "16.10.14",
                   }
              ],
"Description":  "Employee details"

and another JSON file as:

"type": "Employee",
"Properties": [
    {
    "Name": "Ram",
    "Id": "44000",
    "email":  "emp2@company.com",
    "Position":  "Admin",
    "DateOfJoining":  "10.12.14",
    },      
    {
    "Name": "Paul",
    "Id": "44002",
    "email":  "emp3@company.com",
    "Position":  "Programmer",
    "DateOfJoining":  "10.9.14",
    },
],
"Description": "Employee details"

I want to copy the arrays from 1st JSON file to the 2nd JSON file.

You can try something like this:

$c1 = Convert-FromJson (gc file1.json -raw)
$c2 = Convert-FromJson (gc file2.json -raw)
$c3 = $c1.Properties + $c2.Properties
$c3 | ConvertTo-Json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM