Im trying to create a json object from my POST data. When i var_dump the $postData i get the folowing result:
string(92) "{
"name" : "bier",
"city" : "Rotterdam"
"address": "straat 41"
"max_persons": "150"
}"
When i var_dump $json i get NULL
this is my code:
$postData = file_get_contents("php://input");
$json = json_decode($postData);
var_dump($postData);
This is what i am sending to my webservice:
{
"name" : "bier",
"city" : "Rotterdam"
"address": "straat 41"
"max_persons": "150"
}
Why is Json_decode returning NULL?
From the documentation:
NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
Now see JSON Lint:
Parse error on line 3:
... "city": "Rotterdam""address": "straat 4
-----------------------^
Expecting '}', ':', ',', ']'
The JSON is invalid. There is a comma missing between "Rotterdam" and "address"
You are missing a comma in your json, after "straat 41".
{ "name" : "bier", "city" : "Rotterdam" "address": "straat 41", "max_persons": "150" }
Use jsonlint.com to validate the json you are trying to decode.
Related
I have a JSON output in my API response,
Like this:-
{ "m_time" : "2015-04-07 11:37:35", "id" : "30", "msg" : "Hai there 1"}
{ "m_time" : "2015-04-07 11:37:36", "id" : "31", "msg" : "Hai there 2"}
{ "m_time" : "2015-04-07 11:37:37", "id" : "32", "msg" : "Hai there 3"}
I have tried with JSON decode but it returns "null" &
I can't substr from response because it doesn't have any "," like this to separate each response (and not possible to change anything from API response)
How can I access each response from the data??
Since JSON itself is not "streamable" (you can't add data on the end of a valid JSON document and still have a valid JSON document), there is a common format sometimes called "JSON lines": each line of data is a separate JSON document.
To process that data, all you need to do is split the string on newlines (e.g. using explode), then use a foreach loop to pass each line in turn to json_decode.
I'm inserting value with implode and get this type json
[
{
"id":"1",
"f_name":"Mitrajit",
"l_name":"Samanta",
"class":"XII",
"section":"A,B,C",
"roll":"1",
"status":"1"
}
]
but I want this type :-
[
{
"f_name" : "Mitrajit",
"l_name" : "Samanta",
"class" : "XII",
"section": ["A","B","C"],
"roll" : "1",
"status" : "1"
}
]
how can I get "A,B,C" to ["A","B","C"]?
You need to use json_decode() to converting json to php array. Then select section value in json array and use explode() to converting it to array. At the end convert php array to json using json_encode()
$json = json_decode($jsonStr, true);
$json[0]["section"] = explode(",", $json[0]["section"]);
$jsonStr = json_encode($json);
Check result in demo
From MySQL row, I have this json formatted data:
$row['details'] =
{
"previous_employer":[
{"employer":"string1","address":"address1"},
{"employer":"string2","address":"address2"},
{"employer":"string3","address":"address3"}],
"profile":[
"firstname":"John",
"lastname":"Adams",
"gender":"male",
"age":"35",
"contact":"123456789"]
}
I want to extract employer and address on the previous_employer array of objects,
but when I do this:
$json = json_decode($row['details'],true); //decode into array
foreach($json['previous_employer'] as $d){
echo "employer:".$d['employer']."<br>address:". $d['address']."<br>";
}
it gives me an error of
Warning: Invalid argument supplied for foreach()
How can I fix this? Pls advise.. thanks!
You JSON is invalid, "profile" must be
An Object:
"profile": {
"firstname": "John",
"lastname": "Adams",
"gender": "male",
"age": "35",
"contact": "123456789"
}
or an Array of Object (here his length == 1)
"profile": [{
"John",
"Adams",
"male",
"35",
"123456789"
}]
or a Simple Array (not associative array/map)
"profile": [
"John",
"Adams",
"male",
"35",
"123456789"
]
Now, your posted code will work as a charm without any modifications ... :)
json_decode() does not necessarily succeed (just imagine you feed it with complete garbage, why should it return something?). You need to do the following error checking:
Verify its return value:
Returns the value encoded in json in appropriate PHP type. Values
true, false and null are returned as TRUE, FALSE and NULL
respectively. NULL is returned if the json cannot be decoded or if the
encoded data is deeper than the recursion limit.
If valid data can be expected to return null some times, call json_last_error() and check whether it's JSON_ERROR_NONE.
Last but not least, you can explore any variable with var_dump(). You don't need to make assumptions about its content.
I want to decode json string including array and object in PHP. When i decoded with
$array = json_decode($json, true);
print_r($array);
it return NULL. Let me know, the way to decode json in PHP. This is my json string.
{
success: 1,
message: "Successful!",
save_date: "2013-09-11 04:09:26",
test: [
{
test_id: "1",
test_date: "2013-09-12",
test_name: "Test 1"
},
{
test_id: "2",
test_date: "2013-09-11",
test_name: "Test 2"
}
]
}
That's not a valid JSON object. JSON objects must enclose all property names in double quotes:
{ "success": 1, "message": "Successful!" }
PHP provides the handy json_last_error_msg function to tell you that.
There's also the online tool JSONLint to validate JSON strings.
Your JSON is invalid, the property names need to be in quotes too.
Like this:
{
"success": 1,
"message": "Successful!",
"save_date": "2013-09-11 04:09:26",
"test": []
}
Hint: use JSONLint to validate your JSON.
your json string should be like the following :
$sJson = '{"success": 1,"message": "Successful!","save_date": "2013-09-11 04:09:26",
"test": [ {"test_id": "1","test_date": "2013-09-12","test_name": "Test 1"},
{"test_id": "2","test_date": "2013-09-11","test_name": "Test 2"}]}';
Use this
$result=(array)json_decode('your json string');
I think it's working for you
I have passed JSON encoded parameters by POST which we have captured and decoded in another PHP file. I have used the following code to do that.
$entityBody = file_get_contents('php://input');
$entityBody = json_decode($entityBody, true);
I have passed the JSON encoded parameters as follows:
{
"id": "5",
"name": "abcd",
"imei": "1234"
}
Here my code works perfectly fine. However, I want to get all the parameters into a single object so that we can store them efficiently because otherwise there will be too many ifs and elses to get each parameter. So I have encoded the parameters as follows:
device = {
"id": "5",
"name": "abcd",
"imei": "1234"
}
But it is not working. Being new to JSON and PHP I do not know how to handle such cases. How can I achieve this?
use json_decode($_POST['device'], true) since your actually passing a parameter called 'device' to the php file.
You should pass json objects as follow:
{"device" : {
"id": "5",
"name": "abcd",
"imei": "1234"
}}
or if you have an array of devices
{"device" : [{
"id": "5",
"name": "abcd",
"imei": "1234"}
]}