简体   繁体   中英

Merging two json files into one?

I have two .Json files I need to merge into one .Json file:

The first file start.json looks like this:

{
    "MOUSE GESTURE L Start": {
        "prefix": "MOUSE GESTURE L Start To Start",
        "body": [
    "MG_Start()"
        ],
        "description": "Start Cursor back to its start"
    }
}

The second file end.json looks like this:

{
    "MOUSE GESTURE L Move To end": {
        "prefix": "MOUSE GESTURE L Move To end",
        "body": [
    "MG_Move()"
        ],
        "description": "Move Cursor back to its end"
    }
}

The Expected out I am aiming for is:

{
    "MOUSE GESTURE L Start": {
        "prefix": "MOUSE GESTURE L Start To Start",
        "body": [
    "MG_Start()"
        ],
        "description": "Start Cursor back to its start"
    }
    "MOUSE GESTURE L Move To end": {
        "prefix": "MOUSE GESTURE L Move To end",
        "body": [
    "MG_Move()"
        ],
        "description": "Move Cursor back to its end"
    }   
}

I have tried:

$Jstart = (Get-Content .\start.json -Raw) | ConvertFrom-Json
$JEnd = (Get-Content .\end.json -Raw) | ConvertFrom-Json

$Hash = [PSCustomObject]@{
    $Jstart = (Get-Content .\start.json -Raw) | ConvertFrom-Json
    $JEnd = (Get-Content .\end.json -Raw) | ConvertFrom-Json
}

but I get a undesired jumbled output:

{
  "@{MOUSE GESTURE L Start=}": {
    "MOUSE GESTURE L Start": {
      "prefix": "MOUSE GESTURE L Start To Start",
      "body": "MG_Start()",
      "description": "Start Cursor back to its start"
    }
  },
  "@{MOUSE GESTURE L Move To end=}": {
    "MOUSE GESTURE L Move To end": {
      "prefix": "MOUSE GESTURE L Move To end",
      "body": "MG_Move()",
      "description": "Move Cursor back to its end"
    }
  }
}

I've tried variations but things just get worse. Any ideas on how I can get this right? Thank you


EDIT:

@Santiago Squarzon

While solution works for the example I provided, I am getting problems trying to adapt it to my code.

The prblem I am having is, if one of the .Json files happens to have more than one object in it, like this:

{
    "MOUSE GESTURE L Move To end": {
        "prefix": "MOUSE GESTURE L Move To end",
        "body": [
    "MG_Move()"
        ],
        "description": "Move Cursor back to its end"
    },

    "Another Snippet": {
        "prefix": "MOUSE GESTURE L Move To end",
        "body": [
    "MG_Move()"
        ],
        "description": "Move Cursor back to its end"
    }
}

Then $left.PSObject.Properties.Add($($right.PSObject.Properties)) failss, I get the error:

MethodException: 
Cannot find an overload for "Add" and the argument count: "1".

Probably the easiest to go about this would be to append one object to the other:

$left  = Get-Content .\start.json -Raw | ConvertFrom-Json
$right = Get-Content .\end.json -Raw | ConvertFrom-Json
foreach($prop in $right.PSObject.Properties) {
    $left.PSObject.Properties.Add($prop)
}
$left | ConvertTo-Json -Depth 99

Resulting Json would look something like this:

{
  "MOUSE GESTURE L Start": {
    "prefix": "MOUSE GESTURE L Start To Start",
    "body": [
      "MG_Start()"
    ],
    "description": "Start Cursor back to its start"
  },
  "MOUSE GESTURE L Move To end": {
    "prefix": "MOUSE GESTURE L Move To end",
    "body": [
      "MG_Move()"
    ],
    "description": "Move Cursor back to its end"
  }
}

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