I am trying to pass some JSON keys/values that I have to another JSON I am creating dynamically.
For example, this is the JSON I have in $json_create
{
"Key 1":"Value 1",
"Key 2":"Value 2",
"Key 3":"Value 3",
"Key 4":"Value 4"
}
That comes over file_get_contents
$requestBody = file_get_contents('php://input');
$json_create= json_decode($requestBody);
And this is the JSON I am creating
$final_json = [
$json_create,
"type" => "message",
"elements" => $json_merge,
"actions" => $actions_json
];
echo json_encode($final_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
Which print something like
{
"type": "message",
"elements": [
{
"msg_text": "This is a simple response message"
}
]
}
What I am trying to achieve is
{
"Key 1":"Value 1",
"Key 2":"Value 2",
"Key 3":"Value 3",
"Key 4":"Value 4",
"type": "message",
"elements": [
{
"msg_text": "This is a simple response message"
}
]
}
There is quite a lot on that subject but somehow I could not succeed in implementing it.
json_decode($requestBody, true);to get an array - you are currently getting an object back.