Ethan Allen
Ethan Allen

Reputation: 14835

How do I add a JSON array to existing JSON in PHP?

I have the following...

$json1:

[{
    "id": 1,
    "table_prefix": "movie",
    "display_name": "Movies"
}]

$json2:

[{
    "field_name": "Title",
    "column_name": "title",
    "sort_order": 0,
    "sort_section": 0,
    "required": 1,
    "relationship": "field",
    "table_type": "",
    "view_type": "text",
    "description": "",
    "multi_value": 0,
    "char_count": 255
}, {
    "field_name": "Synopsis",
    "column_name": "synopsis",
    "sort_order": 1,
    "sort_section": 0,
    "required": 0,
    "relationship": "field",
    "table_type": "",
    "view_type": "longtext",
    "description": "",
    "multi_value": 0,
    "char_count": 10000
}]

What I want is for $finalJSON to look like this:

[{
    "id": 1,
    "table_prefix": "movie",
    "display_name": "Movies",
    "fields": [{
        "field_name": "Title",
        "column_name": "title",
        "sort_order": 0,
        "sort_section": 0,
        "required": 1,
        "relationship": "field",
        "table_type": "",
        "view_type": "text",
        "description": "",
        "multi_value": 0,
        "char_count": 255
    }, {
        "field_name": "Synopsis",
        "column_name": "synopsis",
        "sort_order": 1,
        "sort_section": 0,
        "required": 0,
        "relationship": "field",
        "table_type": "",
        "view_type": "longtext",
        "description": "",
        "multi_value": 0,
        "char_count": 10000
    }]
}]

Is there a simple PHP JSON function to allow me to attach a JSON array to existing JSON like this?

Upvotes: 4

Views: 4892

Answers (3)

Mulan
Mulan

Reputation: 135197

Is there a simple PHP JSON function to allow me to attach a JSON array to existing JSON like this?

Programming isn't magic. Can you imagine if PHP didn't have arrays or objects to represent your data? What if you only had strings? It would be a nightmare unless the language offered other means of abstraction which would allow us to represent out data in other ways.

It's true that a string could contain the byte data for a song or an image. But would you be asking, "is there a function to raise the volume of a string?" or "can I rotate image contents of a string?" No. You would first decode the contents of the string into a properly abstracted data structure. Then you would use functions that exist to work on the data in that specific domain.

It takes little effort to observe that JSON is nothing more than a string of data. And therefore, we should avoid manipulations on data strings because other data structures offer much better means of manipulation.

In this answer, we will take the data out of the string and populate PHP Arrays and Objects. Because JSON is such a popular format, PHP provides json_decode to do this for us. With the decoded data, we can add the second dataset to the first with a simple array and object property assignment. Lastly, re-encode the data using json_encode to get your intended result.

$json1 = '[{
    "id": 1,
    "table_prefix": "movie",
    "display_name": "Movies"
}]';

$json2 = '[{
    "field_name": "Title",
    "column_name": "title",
    "sort_order": 0,
    "sort_section": 0,
    "required": 1,
    "relationship": "field",
    "table_type": "",
    "view_type": "text",
    "description": "",
    "multi_value": 0,
    "char_count": 255
}, {
    "field_name": "Synopsis",
    "column_name": "synopsis",
    "sort_order": 1,
    "sort_section": 0,
    "required": 0,
    "relationship": "field",
    "table_type": "",
    "view_type": "longtext",
    "description": "",
    "multi_value": 0,
    "char_count": 10000
}]';

$obj1 = json_decode($json1);
$obj1[0]->fields = json_decode($json2);
$finalJson = json_encode($obj1, JSON_PRETTY_PRINT);
echo $finalJson;

[
    {
        "id": 1,
        "table_prefix": "movie",
        "display_name": "Movies",
        "fields": [
            {
                "field_name": "Title",
                "column_name": "title",
                "sort_order": 0,
                "sort_section": 0,
                "required": 1,
                "relationship": "field",
                "table_type": "",
                "view_type": "text",
                "description": "",
                "multi_value": 0,
                "char_count": 255
            },
            {
                "field_name": "Synopsis",
                "column_name": "synopsis",
                "sort_order": 1,
                "sort_section": 0,
                "required": 0,
                "relationship": "field",
                "table_type": "",
                "view_type": "longtext",
                "description": "",
                "multi_value": 0,
                "char_count": 10000
            }
        ]
    }
]   

To @andrew that seems to be struggling to parse the output – please review and run the following code snippet. It parses just fine.

console.log(JSON.parse(`[
    {
        "id": 1,
        "table_prefix": "movie",
        "display_name": "Movies",
        "fields": [
            {
                "field_name": "Title",
                "column_name": "title",
                "sort_order": 0,
                "sort_section": 0,
                "required": 1,
                "relationship": "field",
                "table_type": "",
                "view_type": "text",
                "description": "",
                "multi_value": 0,
                "char_count": 255
            },
            {
                "field_name": "Synopsis",
                "column_name": "synopsis",
                "sort_order": 1,
                "sort_section": 0,
                "required": 0,
                "relationship": "field",
                "table_type": "",
                "view_type": "longtext",
                "description": "",
                "multi_value": 0,
                "char_count": 10000
            }
        ]
    }
]`));

Upvotes: 3

Indrasis Datta
Indrasis Datta

Reputation: 8618

The idea is to need to convert the JSON to array using json_decode, manipulate the fields, and encode it back in JSON format.

Try this:

$decoded_json = json_decode($json1, true);
$decoded_json[0]['fields'] = json_decode($json2, true);
$finalJSON = json_encode($decoded_json, JSON_PRETTY_PRINT);

Upvotes: 3

andrew
andrew

Reputation: 9583

Thats not json. its an array of objects

You can achieve what you want simply with

$json1[0]->fields = $json2;

Upvotes: 3

Related Questions