Merge/Combine JSON objects using Array in PowerShell

In which Scenario do we use JSON object?

Mostly When REST APIs return data in JSON format, you can get at it through PowerShell.So there you have it, working with JSON data can be easy once you turn it into PowerShell objects. Hopefully, this helps you work with APIs and process data on yours.

Recently I was processing some JSON files for one of my projects and wanted to merge multiple similar JSON data into a single JSON.  All the data I was processing were in a similar JSON format, however, coming from different sources. This post will discuss simple JSON data and how we can combine it with single JSON data.

You can also read Application Pool Monitoring Automation – Powershell to check if an application pool has stoppedCreating a scheduled task with PowerShell

Step #1

Declare/Get the JSON data and assigned it to the variable. To make a simple example, I had created two variables to contain the EMP details based on Countries and want to combine those data to have consolidated Employee details.

$Ind_EMPDetails = ‘{ “Indian_EMPDetails” : [ { “firstName”: “Mike”, “lastName”: “John” }, { “firstName”: “Joseph”, “lastName”: “laz” } ] }’

$UK_EMPDetails = ‘{ “UK_EMPDetails” : [ { “firstName”: “Peter”, “lastName”: “Joe” } ] }’

Step #2

Declaring the Empty array for Merging the two JSON objects (ie., $Ind_EMPDetails & $UK_EMPDetails)

$totalEMPDetails = @()

Step #3

Merge the two JSON objects using the ConvertFrom-Json cmdlet. As we are aware, Powershell can’t merge the Json object directly and we need to perform by converting it into the custom PSCustomObject object. We need to use ConvertFrom-Json cmdlet for Combining the JSON objects, ConvertFrom-Json cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom PSCustomObject object that has a property for each field in the JSON string.

$totalEMPDetails += ConvertFrom-Json $Ind_EMPDetails
$totalEMPDetails += ConvertFrom-Json $UK_EMPDetails

Step #4

After Merging, we can use ConvertTo-Json cmdlet to converting t the Array to a JSON object. Finally, print the output in JSON format.

$totalEMPDetails = $totalEMPDetails | ConvertTo-Json
$totalEMPDetails

Full Example

############################################################################
#Project: How to Combine JSON objects using Array in PowerShell
#Developer: Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 [irp]
#E-Mail: mail2thiyaguji@gmail.com 
###########################################################################

$totalEMPDetails = @()

$Ind_EMPDetails = '{ "Indian_EMPDetails" : [ { "firstName": "Mike", "lastName": "John" }, { "firstName": "Joseph", "lastName": "laz" } ] }'
$UK_EMPDetails = '{ "UK_EMPDetails" : [ { "firstName": "Peter", "lastName": "Joe" } ] }'

$totalEMPDetails += ConvertFrom-Json  $Ind_EMPDetails
$totalEMPDetails += ConvertFrom-Json  $UK_EMPDetails

$totalEMPDetails = $totalEMPDetails | ConvertTo-Json
$totalEMPDetails

OUTPUT:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.