I have a file data.json that looks like
{
"18.01": [{
"name": "Title 1",
"id": "0",
"content": {
"html": "This should be 18.01 video 1",
"link": "https://www.youtube.com/embed/Dkm8Hteeh6M"
},
}, {
"name": "test_video_2",
"id": "1",
"content": {
"html": "This should be 18.01 video 2",
"link": "https://www.youtube.com/embed/KWafZrA5OBc"
},
}],
"18.02": [{
"name": "test_video_3",
"id": "0",
"content": {
"html": "This should be 18.02 video 3",
"link": "https://www.youtube.com/embed/EMELKZhM2-g"
},
}, {
"name": "test_video_4",
"id": "1",
"content": {
"html": "This should be 18.02 video 4",
"link": "https://www.youtube.com/embed/TkWmK-cplV4"
},
}]
}
Then I have a php file in the same folder that looks like:
<?php
//Decode JSON
$json_data = json_decode(file_get_contents('data.json'), TRUE);
//Print data
print_r($json_data);
?>
But when I load this php file in my browser I get a blank page. My php service is running (other php works fine), and if I print just the file that I'm loading that displays fine as a string.
Edit: If you're getting this error remember to validate your JSON unlike me...
The above JSON is syntactically wrong. The valid JSON is :
{
"18.01": [
{
"name": "Title 1",
"id": "0",
"content": {
"html": "This should be 18.01 video 1",
"link": "https://www.youtube.com/embed/Dkm8Hteeh6M"
}
},
{
"name": "test_video_2",
"id": "1",
"content": {
"html": "This should be 18.01 video 2",
"link": "https://www.youtube.com/embed/KWafZrA5OBc"
}
}
],
"18.02": [
{
"name": "test_video_3",
"id": "0",
"content": {
"html": "This should be 18.02 video 3",
"link": "https://www.youtube.com/embed/EMELKZhM2-g"
}
},
{
"name": "test_video_4",
"id": "1",
"content": {
"html": "This should be 18.02 video 4",
"link": "https://www.youtube.com/embed/TkWmK-cplV4"
}
}
]
}
Trailing commas are not allowed in a JSON, like at line 8 in the JSON string mentioned with the question
Furthermore, to validate JSON, you could use this nice website
Related
I have this string that was working with json_decode() with PHP 5.6 but doesn't work with PHP 7.2?
$json = '
{
"text": "Hi {{fname}}
Welcome to our customer support.
Please select language to proceed",
"buttons": [
{
"text": "English",
"value": "language_english",
"type": "postback"
},
{
"text": "Germany",
"value": "language_germany",
"type": "postback"
}
]
}';
I have tried replacing the whitespaces and newline like this
$json = preg_replace("/\n/m", '\n', $json);
$what = "\\x00-\\x19"; // all whitespace characters except space itself
$json = trim(preg_replace( "/[".$what."]+/" , '' , $json));
Which results into a string like this
\n{\n "text": "Hi {{fname}} \n Welcome to our customer support. \n Please select language to proceed",\n "buttons": [\n {\n "text": "English",\n "value": "language_english",\n "type": "postback"\n },\n {\n "text": "Germany",\n "value": "language_germany",\n "type": "postback"\n }\n ]\n}
Notice the \n in between and outside double quotes which makes it an invalid json and hence json_decode won't work in this case.
Does anyone know a way to achieve this?
Thank you.
{
"text": "Hi {{fname}} \n Welcome to our customer support. \n Please select language to proceed",
"buttons": [{
"text": "English",
"value": "language_english",
"type": "postback"
},
{
"text": "Germany",
"value": "language_germany",
"type": "postback"
}
]
}
This is a valid json. i added line breaks so you can use them if you want to print the messages in browser.
You can use this nice tool to validate your json when you have doubts.
Editing my answer based on comment feedback.
First of all the right step is that you figure out why you have a broken json in the database. If it's not on you and you have to fix it in php then a solution could be like this:
<?php
echo '<pre>';
$data = '
{
"text": "Hi {{fname}}
Welcome to our customer support.
Please select language to proceed",
"buttons": [
{
"text": "English",
"value": "language_english",
"type": "postback"
},
{
"text": "Germany",
"value": "language_germany",
"type": "postback"
}
]
}';
if (strstr($data, "\n")) {
$data = trim(preg_replace('/\s\s+/', ' ', $data));
}
echo $data;
Above code will capture the line breaks of your text field, and replace them with DOUBLE space. Then you will get a valid json like:
{
"text": "Hi {{fname}} Welcome to our customer support. Please select language to proceed",
"buttons": [
{
"text": "English",
"value": "language_english",
"type": "postback"
},
{
"text": "Germany",
"value": "language_germany",
"type": "postback"
}
]
}
What you can do if you need the line breaks is that you decode your json (just like you want and replace the double spacing in text field with a line break
I am trying to extract the different image URLs from the Instagram media feed:
<?php
$insta = file_get_contents('https://www.instagram.com/apple/media/');
$json = json_decode($insta, true);
foreach($json->items->images as $instaimage){
echo $instaimage['url']['standard_resolution'];
}
?>
Warning: Invalid argument supplied for foreach()
Output from https://www.instagram.com/apple/media:
{"items": [{"id": "1595195862853610235_5821462185", "code": "BYjRi-Aj-r7", "user": {"id": "5821462185", "full_name": "apple", "profile_picture": "https://scontent-bru2-1.cdninstagram.com/t51.2885-19/s150x150/20635165_1942203892713915_5464937638928580608_a.jpg", "username": "apple"}, "images": {"thumbnail": {"width": 150, "height": 150, "url": "https://scontent-bru2-1.cdninstagram.com/t51.2885-15/s150x150/e35/21149363_134897580459546_4211564004683808768_n.jpg"}, "low_resolution": {"width": 320, "height": 320, "url": "https://scontent-bru2-1.cdninstagram.com/t51.2885-15/s320x320/e35/21149363_134897580459546_4211564004683808768_n.jpg"}, "standard_resolution": {"width": 640, "height": 640, "url": "https://scontent-bru2-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/21149363_134897580459546_4211564004683808768_n.jpg"}}, "created_time": "1504382187", "caption": {"id": "17887744828075147", "text": "\u201cI will do almost anything to get the shot I envision...
Hope this one will be helpful. There are few issues with your code.
1. json_decode($insta,true) will give you an array instead of an object, because here you are passing second parameter as true.
2. $json->items contains array of objects of items instead of single items key.
$insta = file_get_contents('https://www.instagram.com/apple/media/');
$json = json_decode($insta);
foreach($json->items as $instaimage)
{
echo $instaimage->images->standard_resolution->url;
echo PHP_EOL;
}
I'm a beginner in coding and i need your help.
I'm trying to have a valide json but i have this error :
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 6 column 35 of the JSON data
I tried this regex in my php file:
preg_match_all("/<[\s]*meta[\s]*name='?" . "([^>']*)'?[\s]*" . "content='?([^>']*)'?[\s]*[\/]?[\s]*>/si", $contents, $match);
And my Json give me that (see the pic in attach to see what's inside "html"):
`{
"CMS": "tumblr",
"title": "Le Blog de Betty - Le Blog de Betty : Blog mode, blog tendances, photos de mode par Betty Autier",
"metaTags": {
"viewport": {
"html": "",
"content": "width=device-width,minimum-scale=1,maximum-scale=1"
},
"description": {
"html": "",
"content": "Le Blog de Betty : Blog mode, blog tendances, photos de mode par Betty Autier"
},
"generator": {
"html": "",
"content": "WPML ver:3.1.8.3 stt:4,1,2,65;0"
},
"og:locale": {
"html": "",
"content": "fr_FR"
},
"og:type": {
"html": "",
"content": "website"
},
"og:title": {
"html": "",
"content": "Le Blog de Betty - Le Blog de Betty : Blog mode, blog tendances, photos de mode par Betty Autier"
},
"og:description": {
"html": "",
"content": "Le Blog de Betty : Blog mode, blog tendances, photos de mode par Betty Autier"
},
"og:url": {
"html": "",
"content": "http://leblogdebetty.com/"
},
"og:site_name": {
"html": "",
"content": "Le Blog de Betty"
},
"article:publisher": {
"html": "",
"content": "http://facebook.com/leblogdebetty"
}
},
"links": {
"shortcut icon": {
"html": "",
"href": "http://localhost:8888/images/2013/12/favicon.ico"
},
"canonical": {
"html": "",
"href": "http://leblogdebetty.com/"
},
"next": {
"html": "",
"href": "http://leblogdebetty.com/page/2/"
},
"publisher": {
"html": "",
"href": "https://plus.google.com/+leblogdebetty"
},
"'dns-prefetch' ": {
"html": "",
"href": "'//s.w.org' /"
},
"stylesheet": {
"html": "",
"href": "http://leblogdebetty.com/wp-content/plugins/jetpack/css/jetpack.css?ver=3.4.3"
},
"'https://api.w.org/' ": {
"html": "",
"href": "'http://leblogdebetty.com/wp-json/' /"
}
}
}
`
Part of Json file
Can someone help me to solve that please ?
Use htmlentities() function on the appropriate fields to convert the quotationmarks to their " representation.
You can also try addslashes() which escapes from " to \".
I have the PHP which should get the JSON object from the postcodes api (http://postcodes.io/) however it does not seem to be working.
if(isset($_POST["postcode"])){
$json = file_get_contents('api.postcodes.io/postcodes/'.$_POST["postcode"]);
$json_data = json_decode($json, true);
echo "My token: ". $json_data["status"];
}
the $json_data["status"]; does not echo anything.
for example when $_POST["postcode"] equals "IP12 2UH" the below should be returned:
{
"status": 200,
"result": {
"postcode": "IP12 2UH",
"quality": 1,
"eastings": 633715,
"northings": 253024,
"country": "England",
"nhs_ha": "East of England",
"longitude": 1.41299500121177,
"latitude": 52.126073695307,
"parliamentary_constituency": "Suffolk Coastal",
"european_electoral_region": "Eastern",
"primary_care_trust": "Suffolk",
"region": "East of England",
"lsoa": "Suffolk Coastal 007G",
"msoa": "Suffolk Coastal 007",
"nuts": null,
"incode": "2UH",
"outcode": "IP12",
"admin_district": "Suffolk Coastal",
"parish": "Suffolk Coastal",
"admin_county": "Suffolk",
"admin_ward": "Rendlesham",
"ccg": "NHS Ipswich and East Suffolk",
"codes": {
"admin_district": "E07000205",
"admin_county": "E10000029",
"admin_ward": "E05007216",
"parish": "E04009449",
"ccg": "E38000086"
}
}
}
You need to add http:// in the url
This question already has answers here:
How to remove backslash on json_encode() function?
(12 answers)
Closed 9 years ago.
This is my original json file
data.json (before)
[
{ "thumb": "../userfiles/img/min/m_1.jpg", "image": "../userfiles/img/1.jpg", "title": "Image 1", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_2.jpg", "image": "../userfiles/img/2.jpg", "title": "Image 2", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_3.jpg", "image": "../userfiles/img/3.jpg", "title": "Image 3", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_4.jpg", "image": "../userfiles/img/4.jpg", "title": "Image 4", "folder": "Folder 1" },
{ "thumb": "../userfiles/img/min/m_5.jpg", "image": "../userfiles/img/5.jpg", "title": "Image 5", "folder": "Folder 1" }
]
This is my php file that adds an element to the file data.jon
add_json.php
<?
$dir_thumb = '../userfiles/img/thumb/6_m.jpg';
$dir_img = '../userfiles/img/6.jpg';
$title = 'img_6';
$tipo_image = 'news';
$json = file_get_contents('data.json');
$data = json_decode($json);
$data[] = array(
'thumb'=> $dir_thumb,
'image'=> $dir_img,
'title'=> $title,
'folder'=> $tipo_image
);
file_put_contents('data.json', json_encode($data));
?>
This is the resulting file data.json
data.json (after executing the file add_json.php)
[
{"thumb":"..\/userfiles\/img\/min\/m_1.jpg","image":"..\/userfiles\/img\/1.jpg","title":"Image 1","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_2.jpg","image":"..\/userfiles\/img\/2.jpg","title":"Image 2","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_3.jpg","image":"..\/userfiles\/img\/3.jpg","title":"Image 3","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_4.jpg","image":"..\/userfiles\/img\/4.jpg","title":"Image 4","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/min\/m_5.jpg","image":"..\/userfiles\/img\/5.jpg","title":"Image 5","folder":"Folder 1"},
{"thumb":"..\/userfiles\/img\/thumb\/6_m.jpg","image":"..\/userfiles\/img\/6.jpg","title":"img_6","folder":"news"}
]
What I do to remove backslashes? Thanks for your help
You can prevent that with the JSON_UNESCAPED_SLASHES option to json_encode, see the documentation.
Note, though, that it's just PHP overkill. The JSON is still valid, and the strings have exactly the same sequence of characters in them that they would have without the unnecessary escape characters.
You don't need to remove those backslashes, they're just escaping characters.
After reading that JSON formatted string, those sequences of \/ will be automatically converted to /.
What you have is perfectly valid JSON, but if you want to remove it, you can do it with the JSON_UNESCAPED_SLASHES constant:
file_put_contents('data.json', json_encode($data, JSON_UNESCAPED_SLASHES));