json_decode: 'Syntax error' for valid JSON from HEREDOC string - php

I have the JSON code below. According to one or two JSON validators it is valid JSON.
{
"patterns": {
"email": "/[a-z0-9._%+-]+#[a-z0-9.-]+\\.[a-z]{2,}/i",
"phone": "/(?:(?:\\(?(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)44\\)?[\\s-]?(?:\\(?0\\)?[\\s-]?)?)|(?:\\(?0))(?:(?:\\d{5}\\)?[\\s-]?\\d{4,5})|(?:\\d{4}\\)?[\\s-]?(?:\\d{5}|\\d{3}[\\s-]?\\d{3}))|(?:\\d{3}\\)?[\\s-]?\\d{3}[\\s-]?\\d{3,4})|(?:\\d{2}\\)?[\\s-]?\\d{4}[\\s-]?\\d{4}))(?:[\\s-]?(?:x|ext\\.?|\\#)\\d{3,4})?/"
}
}
However, when I try to decode using in PHP using the json_decode function I get a 'Syntax Error'. Here's my PHP code:
const JSON_CONFIG = <<<JSON
{
"patterns": {
"email": "/[a-z0-9._%+-]+#[a-z0-9.-]+\\.[a-z]{2,}/i",
"phone": "/(?:(?:\\(?(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)44\\)?[\\s-]?(?:\\(?0\\)?[\\s-]?)?)|(?:\\(?0))(?:(?:\\d{5}\\)?[\\s-]?\\d{4,5})|(?:\\d{4}\\)?[\\s-]?(?:\\d{5}|\\d{3}[\\s-]?\\d{3}))|(?:\\d{3}\\)?[\\s-]?\\d{3}[\\s-]?\\d{3,4})|(?:\\d{2}\\)?[\\s-]?\\d{4}[\\s-]?\\d{4}))(?:[\\s-]?(?:x|ext\\.?|\\#)\\d{3,4})?/"
}
}
JSON;
$config = json_decode(mb_convert_encoding(JSON_CONFIG, "UTF-8"), true); // Tried called trim but it made no difference
echo 'json_last_error_msg() => ' . json_last_error_msg() . PHP_EOL;
print_r($config); // Doesn't get to run
Try for yourself: https://repl.it/#DanStevens/PHP-jsondecode-Syntax-Error
Any ideas what json_decode isn't liking this valid JSON? Is it related to the use of HEREDOC?

The backslashes are acting as PHP escape sequences, not JSON escape sequences. To prevent PHP escaping, surround your heredoc start token in single quotes:
const JSON_CONFIG = <<<'JSON'
{
"patterns": {
"email": "/[a-z0-9._%+-]+#[a-z0-9.-]+\\.[a-z]{2,}/i",
"phone": "/(?:(?:\\(?(?:0(?:0|11)\\)?[\\s-]?\\(?|\\+)44\\)?[\\s-]?(?:\\(?0\\)?[\\s-]?)?)|(?:\\(?0))(?:(?:\\d{5}\\)?[\\s-]?\\d{4,5})|(?:\\d{4}\\)?[\\s-]?(?:\\d{5}|\\d{3}[\\s-]?\\d{3}))|(?:\\d{3}\\)?[\\s-]?\\d{3}[\\s-]?\\d{3,4})|(?:\\d{2}\\)?[\\s-]?\\d{4}[\\s-]?\\d{4}))(?:[\\s-]?(?:x|ext\\.?|\\#)\\d{3,4})?/"
}
}
JSON;
var_dump(JSON_CONFIG);
echo PHP_EOL;
$config = json_decode(mb_convert_encoding(JSON_CONFIG, "UTF-8"), true); // Tried called trim but it made no difference
echo 'json_last_error_msg() => ' . json_last_error_msg() . PHP_EOL;
echo 'json_last_error() => ' . json_last_error() . PHP_EOL;
print_r($config);
Repl: https://repl.it/repls/AmusedOvercookedEmbed

Related

How to put an API return string into an array

I have a string that i get from an API and i wish i could put it in a array so i could check the values that came from the return.
String return example:
{
"code":"000",
"message":"XXX",
"date":"2018-05-17",
"hour":"09:16:09",
"revision":"",
"server":"XX",
"content":{
"nome":{"info":"SIM","conteudo":[{"field1":"XXXX","field2":"XX"}]}
}
}
What I need:
echo $string['code'];
Javascript has no problem with JSON encode command. But how can I do it with PHP?
First of all your JSON data seems to be invalid (Some brackets missing). It needs to be like this:-
{
"code": "000",
"message": "XXX",
"date": "2018-05-17",
"hour": "09:16:09",
"revision": "",
"server": "XX",
"content": {
"nome": {
"info": "SIM",
"conteudo": [{
"field1": "XXXX",
"field2": "XX"
}]
}
}
}
Now You need to decode this JSON data and then get data based on the index
$array = json_decode($json,true);
echo $array['code'];
Output:-https://eval.in/1005949
You can decode the JSON string to Array in PHP.
$str = '{"code":"000","message":"XXX","date":"2018-05-17","hour":"09:16:09","revision":"","server":"XX","content":{"nome":{"info":"SIM","conteudo":[{"field1":"XXXX","field2":"XX"}]}';
$decodedValue = json_decode($str, true);
var_dump($decodedValue);
Your example string is not valid json, you are missing some closing brackets.
This should be the correct way:
'{"code":"000","message":"XXX","date":"2018-05-17","hour":"09:16:09","revision":"","server":"XX","content":{"nome":{"info":"SIM","conteudo":[{"field1":"XXXX","field2":"XX"}]}}}'
As for your question. in PHP you can easily use json_decode
Example:
<?php
$json = '{"code":"000","message":"XXX","date":"2018-05-17","hour":"09:16:09","revision":"","server":"XX","content":{"nome":{"info":"SIM","conteudo":[{"field1":"XXXX","field2":"XX"}]}}}';
$decoded_json = json_decode($json, true);
echo $decoded_json['code'];
You can see it working here

How to Parse this JSON (PHP & JSON_DECODE)

I am using the following API: https://openweathermap.org and it gives a JSON response to get the current weather.
{
"coord":{
"lon":
"lat":
},
"weather":[
{
"id":521,
"main":"Rain",
"description":"shower rain",
"icon":"09n"
}
],
"base":"stations",
"main":{
"temp":289.22,
"pressure":1004,
"humidity":82,
"temp_min":288.15,
"temp_max":290.15
},
"visibility":10000,
"wind":{
"speed":4.1,
"deg":210
},
"clouds":{
"all":100
},
"dt":1501793400,
"sys":{
"type":1,
"id":5060,
"message":0.0039,
"country":"GB",
"sunrise":1501734589,
"sunset":1501790444
},
"id":3333126,
"name":"Borough of Blackburn with Darwen",
"cod":200
}
What is the correct way to go about getting the main and description in the JSON?
I have attempted the code below but it doesn't work:
$url = "http://api.openweathermap.org/data/2.5/weather?lat=" . $latitude . "&lon=" . $longitude . "&APPID=71f4ecbff00aaf4d61d438269b847f11";
$dirty_data = file_get_contents( $url );
$data = json_decode( $dirty_data );
echo $data['weather']['main'];
When using json_decode() to convert json data to php type, it will always convert it into an object. To be clear you can access weather's main and description property like this:
echo $data->weather[0]->main // outputs main
echo $data->weather[0]->description // outputs description
Update:
Besides you can also convert data into an associative array by passing bool(true) $assoc argument to the json_decode() function.
$data = json_decode( $dirty_data, true );
And extract your data like this:
echo $data['weather'][0]['main']; // for main
echo $data['weather'][0]['description']; // for description
Yo can always do var_dump($data) to see what you have and how to access it.
json_decode returns a stdClass not an array.
$data->weather[0]->main;
Should be the right thing.
{} symbolizes an object and [] symbolizes an array. Notice weather:[{...}] which means weather is an array of objects.
Try
$data = json_decode( $dirty data, true)
It doesn't convert to an object if you supply the true argument.

Parse SendGrid JSON with PHP

I have some simple JSON data returned by the SendGrid API:
{
"message":"error",
"errors":[
"some errors"
]
}
I can access the contents of the "message" section via:
$txt = "{\"message\":\"success\"}";
$newtxt = json_decode($txt, true);
echo $newtxt['message'];
That works okay, but I can't work out how to access the contents of the "errors" section?
Sorry, I realise this is probably a silly question.
If your JSON String is this
$tst = '{
"message":"error",
"errors":[
"some errors"
]
}';
Then all you need to so is
$j_array = json_decode($txt, true);
echo $j_array['message'];
echo $j_array['errors'][0];
A better way would be to loop over this array
foreach ($j_array['errors'] as $error ) {
echo $error . '<br>';
}
Of course you dont need to convert everything to an array, you can leave it as written i.e. an object containing properties one of which is an array
$jObj = json_decode($txt);
echo $jObj->message;
echo $jObj->errors[0];
Or
foreach ($jObj->errors as $error ) {
echo $error . '<br>';
}

Parsing JSON object in PHP using json_decode

I tried to request the weather from a web service supplying data in JSON format. My PHP request code, which did not succeed was:
$url="http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
echo $data[0]->weather->weatherIconUrl[0]->value;
This is some of the data that was returned. Some of the details have been truncated for brevity, but object integrity is retained:
{ "data":
{ "current_condition":
[ { "cloudcover": "31",
... } ],
"request":
[ { "query": "Schruns, Austria",
"type": "City" } ],
"weather":
[ { "date": "2010-10-27",
"precipMM": "0.0",
"tempMaxC": "3",
"tempMaxF": "38",
"tempMinC": "-13",
"tempMinF": "9",
"weatherCode": "113",
"weatherDesc": [ {"value": "Sunny" } ],
"weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ],
"winddir16Point": "N",
"winddirDegree": "356",
"winddirection": "N",
"windspeedKmph": "5",
"windspeedMiles": "3" },
{ "date": "2010-10-28",
... },
... ]
}
}
}
This appears to work:
$url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710%22';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['data']['weather'] as $item) {
print $item['date'];
print ' - ';
print $item['weatherDesc'][0]['value'];
print ' - ';
print '<img src="' . $item['weatherIconUrl'][0]['value'] . '" border="0" alt="" />';
print '<br>';
}
If you set the second parameter of json_decode to true, you get an array, so you cant use the -> syntax. I would also suggest you install the JSONview Firefox extension, so you can view generated json documents in a nice formatted tree view similiar to how Firefox displays XML structures. This makes things a lot easier.
If you use the following instead:
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
The TRUE returns an array instead of an object.
Try this example
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
http://php.net/manual/en/function.json-decode.php
NB - two negatives makes a positive . :)
Seems like you forgot the ["value"] or ->value:
echo $data[0]->weather->weatherIconUrl[0]->value;
When you json decode , force it to return an array instead of object.
$data = json_decode($json, TRUE); -> // TRUE
This will return an array and you can access the values by giving the keys.
You have to make sure first that your server allow remote connection so that the function file_get_contents($url) works fine , most server disable this feature for security reason.
While editing the code (because mild OCD), I noticed that weather is also a list. You should probably consider something like
echo $data[0]->weather[0]->weatherIconUrl[0]->value;
to make sure you are using the weatherIconUrl for the correct date instance.

Convert JSON in print format to valid JSON

I have a text file that is formatted like JSON, but in a print/view friendly format and I want to convert that string to valid JSON.
Basically, I want to read the file using PHP5 and call json_decode to deserialize the string.
But, json_decode is not able to parse the "print-friendly" json string.
I am getting error 4 Invalid or malformed JSON.
It looks like someone else had a similar issue as me: PHP json_decode() returns NULL with valid JSON?
I am using notepad++ to write the json file.
So, how can I convert
FROM:
{
"data": [
{
"thumbImg": "thumbImg",
"street": "street",
"city": "Fort Worth",
"state": "Texas",
"zip": "76192-0001",
"url": "url"
}
]
}
TO:
{"data":[{"thumbImg": "thumbImg", "street": "street", "city": "Fort Worth", "state": "Texas", "zip": "76192-0001", "url": "url"}]
I even tried doing the following:
<?php
$filename = "links.json";
$file = fopen($filename, "r");
$lines = file($filename);
$data = "";
;
foreach ($lines as $line_num => $line) {
$formatted = trim($line);
$formatted = str_replace("\r", "", $formatted);
$formatted = str_replace("\n", "", $formatted);
$data .= $formatted;
}
$json = json_decode($data, true);
?>
I did a var_dump of the resulting json string and http://jsonlint.com/ marked it as valid json; however, json_decode is not able to deserialize the json string for some reason.
Thank you!
SOLUTION
I set the encoding of the text file to UTF-8 without BOM and it works fine now. thank you all!
<?php
$filename = "links.json";
$file = file_get_contents($filename);
$json = json_decode($file, true);
?>
References:
- file_get_contents()
- json_decode()

Categories