json_decode + json_encode combo not creating original JSON - php

I have some JSON that I got through an API call, and I run json_decode on it, grab an array from it, then re-encode it with json_encode. The result however is not the same JSON; it's messing up with the URLs. How do I make it encode properly?
original
{"created_at":"Mon, 19 Mar 2012 01:34:41 +0000","entities":{"hashtags":[{"text":"stanford","indices":[23,32]}],"urls":[{"url":"http:\/\/t.co\/Of4z6jKG","expanded_url":"http:\/\/360.io\/5sZc2T","display_url":"360.io\/5sZc2T","indices":[33,53]}],"user_mentions":[]},"from_user":"rayfk","from_user_id":335143881,"from_user_id_str":"335143881","from_user_name":"Raymond Kennedy","geo":{"coordinates":[37.4227,-122.1753],"type":"Point"},"id":181554251733020673,"id_str":"181554251733020673","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1468102095\/image_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1468102095\/image_normal.jpg","source":"<a href="http:\/\/www.occipital.com\/360\/app" rel="nofollow">360 Panorama<\/a>","text":"View from mid lake log #stanford http:\/\/t.co\/Of4z6jKG","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null}
after decode/encode combo
{"created_at":"Mon, 19 Mar 2012 01:34:41 +0000","entities":{"hashtags":[{"text":"stanford","indices":[23,32]}],"urls":[{"url":"http:\/\/t.co\/Of4z6jKG","expanded_url":"http:\/\/360.io\/5sZc2T","display_url":"360.io\/5sZc2T","indices":[33,53]}],"user_mentions":[]},"from_user":"rayfk","from_user_id":335143881,"from_user_id_str":"335143881","from_user_name":"Raymond Kennedy","geo":{"coordinates":[37.4227,-122.1753],"type":"Point"},"id":181554251733020673,"id_str":"181554251733020673","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1468102095\/image_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1468102095\/image_normal.jpg","source":"<a href="http:\/\/www.occipital.com\/360\/app" rel="nofollow">360 Panorama<\/a>","text":"View from mid lake log #stanford http:\/\/t.co\/Of4z6jKG","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null}
Those are the full snippets, but the culprit is this:
original "source":"<a href="http:\/\/www.occipital.com\/360\/app" rel="nofollow">360 Panorama<\/a>"
after "source":"<a href="http:\/\/www.occipital.com\/360\/app" rel="nofollow">360 Panorama<\/a>"

I'm not sure what is causing it but you can correct it by applying the html_entity_decode() function to the after version. This will change things such as < or " back to their original form.
Depending on how it affects your quoting, there are a few flags you can pass it as well to get the result you need.
ENT_COMPAT: Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES: Will convert both double and single quotes.
ENT_NOQUOTES: Will leave both double and single quotes unconverted.
[EDIT]
Run your broken JSON through this function:
function fixDoubleQuotedJSON($broken_json)
{
return str_replace('"','\\"',$broken_json);
}

http://codepad.org/DMkAS2iR
They seem to be equal, except at an unexcepted place:
before: "id":181554251733020673,"id_str"
after: "id":181554251733020000,"id_str"
Those id's won't really match after "lossless" json transforms, and JSON_BIGINT_AS_STRING option is supported from PHP 5.4.
Codepad's php version is 5.2.5 by the way;

Related

Accessing POST data

I'm very new to PHP but have a good understanding of C,
When I want to access some post data on an API i'm creating in PHP I use:
$_POST['date_set']
to fetch a value being passed for date - This all works perfectly, however I read I should be fetching it like this:
$date_set = trim(urldecode($_POST['date_set']));
This always returns a 00:00:00 value for the date after it's stored in my DB.
When I access directly using $_POST['date_set'] I get whatever value was posted, for example: 2013-08-28 10:31:03
Can someone tell me what I'm messing up?
You should try it like,
$date_set = $_POST['date_set'].explode(' ');//('2013-08-28 10:31:03').explode(' ')
echo $date_set[1];
or
echo date('H:i:s',strtotime($_POST['date_set'])));
//echo date('H:i:s',strtotime('2013-08-28 10:31:03'));
If you are very new in php the Read date()
You only run urldecode over data is URL encoded. PHP will have decoded it before populating $_POST, so you certainly shouldn't be using that. (You might have to if you are dealing with double-encoded data, but the right solution there should be to not double encode the data).
trim removes leading and trailing white-space. It is useful if you have a free form input in which rogue spaces might be typed. You will need to do further sanity checking afterwards.
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
urldecode: is used only for GET requests. you should be fine using $_POST['date_set'] only.
http://php.net/manual/en/function.urldecode.php
You'd better do this way
if(isset($_POST['date_set'])){
$date_set = $_POST['date_set'];
}
then you can use $date_set how you want.
If you still get 00:00:00 for $date_set, the problem is coming from the code which provide you the $_POST value.

PHP json_decode returns null

I'm writing PHP code that uses a database. To do so, I use an array as a hash-map.
Every time content is added or removed from my DB, I save it to file.
I'm forced by my DB structure to use this method and can't use mysql or any other standard DB (School project, so structure stays as is).
I built two functions:
function saveDB($db){
$json_db = json_encode($db);
file_put_contents("wordsDB.json", $json_db);
} // saveDB
function loadDB(){
$json_db = file_get_contents("wordsDB.json");
return json_decode($json_db, true);
} // loadDB
When echo-ing the string I get after the encoding or after loading from file, I get a valid json (Tested it on a json viewer) Whenever I try to decode the string using json_decode(), I get null (Tested it with var_dump()).
The json string itself is very long (~200,000 characters, and that's just for testing).
I tried the following:
Replacing single/double-quotes with double/single-quotes (Without any backslashes, with one backslash and three backslashes. And any combination I could think of with a different number of backslashes in the original and replaced string), both manually and using str_replace().
Adding quotes before and after the json string.
Changing the page's encoding.
Decoding without saving to file (Right after encoding).
Checked for slashes and backslashes. None to be found.
Tried addslashes().
Tried using various "Escape String" variants.
json_last_error() doesn't work. I get no error number (Get null, not 0).
It's not my server, so I'm not sure what PHP version is used, and I can't upgrade/downgrade/install anything.
I believe the size has something to do with it, because small strings seem to work fine.
Thanks Everybody :)
In your JSON file change null to "null" and it will solve the problem.
Check if your file is UTF8 encoded. json_decode works with UTF8 encoded data only.
EDIT:
After I saw uploaded JSON data, I did some digging and found that there are 'null' key. Search for:
"exceeding":{"S01E01.html":{"2217":1}},null:{"S01E01.html":
Change that null to be valid property name and json_decode will do the job.
I had a similar problem last week. my json was valid according to jsonlint.com.
My json string contained a # and a & and those two made json_decode fail and return null.
by using var_dump(json_decode($myvar)) which stops right where it fails I managed to figure out where the problem was coming from.
I suggest var_dumping and using find dunction to look for these king of characters.
Just on the off chance.. and more for anyone hitting this thread rather than the OP's issue...I missed the following, someone had htmlentities($json) way above me in the call stack. Just ensure you haven't been bitten by the same and check the html source.
Kickself #124

json_decode returns null with PHP, NFL API

For some reason json_decode is returning NULL with valid JSON.
The json is located here: http://www.nfl.com/liveupdate/scorestrip/scorestrip.json
I am getting the file contents of the page (file_get_contents) and then running json_decode on it.
Also tried stripslashes b/c of magic quotes.
Thanks in advance,
Phil
$json = file_get_contents('http://www.nfl.com/liveupdate/scorestrip/scorestrip.json');
var_dump(json_decode($json));
The empty array elements are invalid JSON, ie the multiple commas with no values between
["Thu","7:30","Final",,"BAL","6","PHI","13",,,"55424",,"PRE1","2011"]
^ Here ^ Here ^Here
You could try inserting empty strings between multiple commas (thanks hakre), ie
$json = preg_replace('/,(?=,)/m', ',""', $json);
// I'm dubious about this working, don't have PHP to test but it works in JS
I don't know if this messes up your ability to continue using the data though
I came the same conclusion as Phil, but it took me about 5 minutes longer. : )
However, I noticed a problem in his output... it is dropping a field.
I don't like doing this kind of pattern detection in RegEx, only because I think it is harder to maintain. In this case, I just run it through a while loop.
while (strpos($json,',,'))
{
$json = str_replace(',,',',"",', $json);
}
I'm sure Phil can come up with a proper RegEx, but this solution will maintain the right # of entries & their ordinal positions.
For anyone looking in 2014. The new feed data link should be:
http://www.nfl.com/liveupdate/scores/scores.json

Not able to parse this json

I am trying to parse the json output from
http://www.nyc.gov/portal/apps/311_contentapi/services/all.json
And my php json_decode returns a NULL
I am not sure where the issue is, I tried running a small subset of the data through JSONLint and it validated the json.
Any Ideas?
The error is in this section:
{
"id":"2002-12-05-22-24-56_000010083df0188b4001eb56",
"service_name":"Outdoor Electric System Complaint",
"expiration":"2099-12-31T00:00:00Z",
"brief_description":"Report faulty Con Edison equipment, including dangling or corroded power lines or "hot spots.""
}
See where it says "hot spots." in an already quoted string. Those "'s should've been escaped. Since you don't have access to edit the JSON perhaps you could do a search for "hot spots."" and replace it with \"hot spots.\"" like str_replace('"hot spots.""', '\\"hot spots.\\""\, $str); for as long as that's in there. Of course that only helps if this is a one time thing. If the site continues to make errors in their JSON output you'll have to come up with something more complex.
What I did to identify the errors in the JSON ...
Since faulty quoting is the first thing to look for, I downloaded the JSON to a text file, opened in a text editor (I used vim but any full featured editor would do), ran a search and replace that removed all characters except double-quote and looked at the result. It was clear that correct lines should have 4 double-quotes so I simply searched for 5 double-quotes together and found the first bad line. I noted the line number and then undid the search and replace to get the original file back and looked at that line. This gives you what you need to get the developers of the API to fix the JSON.
Writing code to automatically fix the bad JSON before giving it to json_decode() would be quite a bit harder but doable using techniques like those in another answer.
According to the PHP manual:
In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error.
Try calling it to see where the error is.

PHP Json Encoding w/ quote escaping in 5.2?

I'm playing with the flickr api and php. I want to pass some information from PHP to Javascript through Ajax. I have the following code:
json_encode($pics);
which results in the following example JSON string:
[{"id":"4363603591","title":"blue, white and red...another seattle view","date_faved":"1266379499"},{"id":"4004908219","title":"\u201cI just told you my dreams and you made me see that I could walk into the sun and I could still be me and now I can't deny nothing lasts forever.\u201d","date_faved":"1259987670"}]
Javascript has problems with this, however, due to the unescaped single-quote in the second item ("can't deny").
I want to use the function json_encode with the options parameter to make it strip the quotes, but that's only available in PHP 5.3, and I'm running 5.2 (not my server). Is there a fast way to run through the entire array and escape everything before encoding it in Json? I looked for a way to do this, but it all seems to deal with encoding it as the data is generated, something I cannot do as I'm not the one generating the data.
If it helps, I'm currently using the following javascript after the ajax request:
var photos = eval('(' + resptxt + ')');
Have you considered using JSON2 instead of eval()? Details here.
str_replace('\'', '\\'', json_encode($pics))
You'll have to do a (recursive) foreach to walk through the array and manipulate them manually. You can do a str_replace, but addslashes works just as fine (and addcslashes is even better.)

Categories