How to create a JSON object

PhpJson

Php Problem Overview


I am trying to create an JSON object out of a PHP array. The array looks like this:

$post_data = array('item_type_id' => $item_type,
    'string_key' => $string_key,
    'string_value' => $string_value,
    'string_extra' => $string_extra,
    'is_public' => $public,
    'is_public_for_contacts' => $public_contacts);

The code to encode the JSON look like this:

$post_data = json_encode($post_data);

The JSON file is supposed to look like this in the end:

{
	"item": {
		"is_public_for_contacts": false,
		"string_extra": "100000583627394",
		"string_value": "value",
		"string_key": "key",
		"is_public": true,
		"item_type_id": 4,
		"numeric_extra": 0
	}
}

How can I encapsulate the created JSON code in the "item": { JSON CODE HERE }.

Php Solutions


Solution 1 - Php

Usually, you would do something like this:

$post_data = json_encode(array('item' => $post_data));

But, as it seems you want the output to be with "{}", you better make sure to force json_encode() to encode as object, by passing the JSON_FORCE_OBJECT constant.

$post_data = json_encode(array('item' => $post_data), JSON_FORCE_OBJECT);

"{}" brackets specify an object and "[]" are used for arrays according to JSON specification.

Solution 2 - Php

Although the other answers posted here work, I find the following approach more natural:

$obj = (object) [
    'aString' => 'some string',
    'anArray' => [ 1, 2, 3 ]
];

echo json_encode($obj);

Solution 3 - Php

You just need another layer in your php array:

$post_data = array(
  'item' => array(
    'item_type_id' => $item_type,
    'string_key' => $string_key,
    'string_value' => $string_value,
    'string_extra' => $string_extra,
    'is_public' => $public,
   'is_public_for_contacts' => $public_contacts
  )
);

echo json_encode($post_data);

Solution 4 - Php

You could json encode a generic object.

$post_data = new stdClass();
$post_data->item = new stdClass();
$post_data->item->item_type_id = $item_type;
$post_data->item->string_key = $string_key;
$post_data->item->string_value = $string_value;
$post_data->item->string_extra = $string_extra;
$post_data->item->is_public = $public;
$post_data->item->is_public_for_contacts = $public_contacts;
echo json_encode($post_data);

Solution 5 - Php

$post_data = [
  "item" => [
    'item_type_id' => $item_type,
    'string_key' => $string_key,
    'string_value' => $string_value,
    'string_extra' => $string_extra,
    'is_public' => $public,
    'is_public_for_contacts' => $public_contacts
  ]
];

$post_data = json_encode(post_data);
$post_data = json_decode(post_data);
return $post_data;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark DennView Question on Stackoverflow
Solution 1 - PhpCristianView Answer on Stackoverflow
Solution 2 - PhptheDmiView Answer on Stackoverflow
Solution 3 - PhpzeusstlView Answer on Stackoverflow
Solution 4 - PhpMiteshView Answer on Stackoverflow
Solution 5 - PhpAlauddin Afif CassandraView Answer on Stackoverflow