I have this json saved in my database in a column called 'price' with the type of 'text'.
{
"desks": {
"Dedicated Desk": "$400 mo"
},
"private offices": {
"1 Person": "$550 mo",
"2 Person": "$1100 mo",
"3 Person": "$1600 mo",
"4 Person": "$2100 mo",
"6 Person": "$3300 mo"
},
"flexible membership": {
"Starting at": "$45 mo",
"Every Extra Day": "$50 Day"
}
}
I then make a call from PHP to return my all the fields in my database and encode them as json.
$json = json_encode($response,JSON_PRETTY_PRINT);
echo $json;
$response is the response from the database. When I var_dump on $response I get
array(22) {
[0]=>
object(stdClass)#6 (38) {
[...]
["price"]=>
string(242) "{"desks":{"Dedicated Desk":"$400 mo"},"private offices":{"1 Person":"$550 mo","2 Person":"$1100 mo","3 Person":"$1600 mo","4 Person":"$2100 mo","6 Person":"$3300 mo"},"flexible membership":{"Starting at":"$45 mo","Every Extra Day":"$50 Day"}}"
[...]
}
[...]
}
When I echo the result from json_encode i get
[
{
[...]
"price": "{\"desks\":{\"Dedicated Desk\":\"$400 mo\"},\"private offices\":{\"1 Person\":\"$550 mo\",\"2 Person\":\"$1100 mo\",\"3 Person\":\"$1600 mo\",\"4 Person\":\"$2100 mo\",\"6 Person\":\"$3300 mo\"},\"flexible membership\":{\"Starting at\":\"$45 mo\",\"Every Extra Day\":\"$50 Day\"}}",
[...]
},
[...]
]
My problem is json_encode is taking the json from the database and formatting it as a string. I am trying to get it to format it as part of a multidimensional json structure. This is what im trying to achieve:
[
{
"price":[
{
"desks":{
"Dedicated Desk":"$400 mo"
},
"private offices":{
"1 Person":"$550 mo",
"2 Person":"$1100 mo",
"3 Person":"$1600 mo",
"4 Person":"$2100 mo",
"6 Person":"$3300 mo"
},
"flexible membership":{
"Starting at":"$45 mo",
"Every Extra Day":"$50 Day"
}
}
]
}
]
Any help would be appreciated. Running latest version of php.
It is encoding it as a string because it IS a string. What you need to do is decode it first.
$response['price'] = json_decode($response['price']);
$json = json_encode($response,JSON_PRETTY_PRINT);
echo $json;