ivor
ivor

Reputation: 317

Joint two or more json files

I have a problem need to join or merge two or more json file..

so far here's my code:

//first
    $url1="https://www.zopim.com/api/v2/chats";
    $ch1 = curl_init();
    curl_setopt($ch1, CURLOPT_URL, $url1);
    curl_setopt($ch1, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
    $output1 = curl_exec($ch1);
    $info1 = curl_getinfo($ch1);
    curl_close($ch1);

    $chats1 = json_decode($output1,true);

    //second
    $url2="https://www.zopim.com/api/v2/chats?page=2";
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_URL, $url2);
    curl_setopt($ch2, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    $output2 = curl_exec($ch2);
    $info2 = curl_getinfo($ch2);
    curl_close($ch2);

    $chats2 = json_decode($output2,true);



    $r = [];
    if(is_array($chats1) && is_array($chats2))
    {
        foreach($chats1 as $key => $array)
        {
            $r[$key] = array_merge($chats2[$key], $array);
        }
    }
    else
    {
    echo 'problem with json';
    }
    echo json_encode($r, JSON_UNESCAPED_SLASHES);

but i encounter an error: enter image description here

here's number 44 error line: enter image description here

hopefully you can help me or do you have a much better code or login for this one.. like using foreach...

I also want to make the link auto generated by number like ?page=1, ?page=2 and so on...

Here's my json: json1 enter image description here

and json2: enter image description here

Upvotes: 1

Views: 136

Answers (4)

harsh kumar
harsh kumar

Reputation: 165

You can do this:

$user = array();
$user[] = json_decode($json1,true);
$user[] = json_decode($json2,true);
$json_merge = json_encode($user);

Upvotes: 0

Reena Mori
Reena Mori

Reputation: 645

If your both array structure same Here you pass $chats2[$key] as string on array_merge(); so error occurred

$res_arr = array_merge($chats2,$chats1);
array_values($res_arr);

$r = [];
if(!empty($res_arr))
{
    foreach($res_arr as $key => $array)
    {
        $r[$key] = $array;
    }
}
else
{
    echo 'problem with json';
}
echo json_encode($r);

Upvotes: 0

Jaydeep Mor
Jaydeep Mor

Reputation: 1703

I have create two small json for join it ! Check below,

Json - 1

{
    "chats": [
        {
            "comment": null,
            "triggered_response": true,
            "visitor": {
                "phone": ""
            },
            "session": {
                "city": "Moncton",
                "end_date": "2017-07-03",
                "ip": "99.240.22.84"
            },
            "duration": "32",
            "agent_names": {
                "name": "test"
            }
        }
    ]
}

Json - 2

{
    "chats": [
        {
            "comment": null,
            "triggered_response": true,
            "visitor": {
                "phone": ""
            },
            "session": {
                "city": "Moncton1",
                "end_date": "2017-08-03",
                "ip": "99.240.22.85"
            },
            "duration": "321",
            "agent_names": {
                "name": "test1"
            }
        }
    ]
}

Now i join that using array_merge_recursive.

PHP

// $json is first json encoded string same as $json1 is second encoded string.
$merge_array = array_merge_recursive(json_decode($json,true),json_decode($json1,true));

Json encoded Output

{
    "chats": [
    {
        "comment": null,
        "triggered_response": true,
        "visitor": {
            "phone": ""
        },
        "session": {
            "city": "Moncton",
            "end_date": "2017-07-03",
            "ip": "99.240.22.84"
        },
        "duration": "32",
        "agent_names": {
            "name": "test"
        }
    },
    {
        "comment": null,
        "triggered_response": true,
        "visitor": {
            "phone": ""
        },
        "session": {
            "city": "Moncton1",
            "end_date": "2017-08-03",
            "ip": "99.240.22.85"
        },
        "duration": "321",
        "agent_names": {
            "name": "test1"
        }
    }
    ]
}

Upvotes: 0

Vishnu Damwala
Vishnu Damwala

Reputation: 151

$finalArray = [];
$finalArray[] = json_decode($json1,true);
$finalArray[] = json_decode($json2,true);
$mergedJSON = json_encode($finalArray);

For same array structure you can use array_merge($array1, $array2) method & then use json_encode().

Example:

$mergedArray = array_merge($array1, $array2);
$mergedJSON = json_encode($mergedArray);

Upvotes: 2

Related Questions