How to Merge Two JSON Strings into One with PHP

On 1/19/2018

Hi! In this tutorial we will see how to merge two json strings into one in php. People often mistakes JSON for object or array but it's the string representation of a JavaScript object. In a recent project, I wanted to combine two jsons. That is, I have to make two API calls and combine their results into one. It is a rest api and returns the response as json. The task is simpler than I thought. It employs a similar technique I used here earlier when dealing with json. Let me show you how to do it.

php merge two json strings

PHP - Merging Two JSON into One:

If you are new to working with JSON, don't worry. PHP offers native functions to handle it easily. The idea is to decode the json data, write it to an array and encode it back to json.

Below are the steps involved in the process.

STEP-1) Let's take two different jsons containing couple of employee details.

<?php
$json1 = '{"id": "GD01", "name": "Garrett Davidson", "position": "System Administrator", "location": "New York"}';
$json2 = '{"id": "DW02", "name": "Donna Winters", "position": "Senior Programmer", "location": "Seattle"}';
?>

STEP-2) Next we should convert the json strings into an array and join them into a single array.

<?php
$array[] = json_decode($json1, true);
$array[] = json_decode($json2, true);
?>

Keep in mind the function json_decode() will return stdObject by default. To make it return associative array, we have to set the second parameter as 'true'.

STEP-3) Then convert the merged array to json. For this we have to use the function json_encode(). While encoding, I have included the token 'JSON_PRETTY_PRINT' to add proper indentation so that it's easy on the eyes.

<?php
$result = json_encode($array, JSON_PRETTY_PRINT);
?>

STEP-4) Finally print the merged json. You can simply echo it, but the output will look messy and difficult to read. So be sure to set the headers with right content type to display indents and blank spaces between the texts.

<?php
header('Content-type: text/javascript');
echo $result;
?>

This will produce the below output,

// output
[
    {
        "id": "GD01",
        "name": "Garrett Davidson",
        "position": "System Administrator",
        "location": "New York"
    },
    {
        "id": "DW02",
        "name": "Donna Winters",
        "position": "Senior Programmer",
        "location": "Seattle"
    }
]

As you can see, the output consists of a nested json combining the data from $json1 and $json2.

Here is the complete php script for json merging process.

index.php

<?php
$json1 = '{"id": "GD01", "name": "Garrett Davidson", "position": "System Administrator", "location": "New York"}';
$json2 = '{"id": "DW02", "name": "Donna Winters", "position": "Senior Programmer", "location": "Seattle"}';

// decode json to array
$array[] = json_decode($json1, true);
$array[] = json_decode($json2, true);

// encode array to json
$result = json_encode($array, JSON_PRETTY_PRINT);

// print merged json
header('Content-type: text/javascript');
echo $result;
?>

You can also store the json output in a file instead of displaying it in the browser window.

Read Also:

That explains about combining two or more json strings in php. The process can be easily done with core php alone and doesn't require any external tool. I hope you like this tutorial. Please share it on social media if you find it useful.

No comments:

Post a Comment

Contact Form

Name

Email *

Message *