base64 json_decode issue - php - php

Fetching BLOB from mysql
$sql = "SELECT data as datajson FROM tablename";
$res=$obj->_executeQuery($sql);
$res=$obj->getAll($res);
$blobdata = $res[0]['datajson'];
My json data from BLOB looks like this
[{"name":"propose","value":"Propose~sample"},
{"name":"expenseamount","value":"Expense Amount~15246"},
{"name":"paymenttype","value":"Payment Type~ Cash"},
{"name":"img0","value":"/9j/4AAQSkZHIjks........61A/3qBo/9k="},
{"name":"img1","value":"NO"}]
Now
{"name":"img0","value":"/9j/4AAQSkZHIjks........61A/3qBo/9k="}
This pair is a base 64 string which is included in my BLOB. And when i did json_decode($blobdata); i am getting null
The exact base64 string can be found in JSFIDDLE
I have added json_last_error(); in my code
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
Which returns - Syntax error, malformed JSON and
json_decode does not work, since this is not a proper json
I want the details other than img0 and img1
1 . Now how do i remove the last two pairs (img0, img1) BEFORE json_decode, so that this becomes valid json.
2 . Or how to convert the base64 string to become proper json
UPDATE
This is how BLOB data is storing in mysql
foreach($_REQUEST as $key=>$value)
{
$raw_data[] = array('name' => $key, 'value' => $value);
}
$json = json_encode($raw_data);
$sql="INSERT INTO tablename (data) VALUES ('$json')";

Related

php json_decode not working from database (pma)

I am trying to use PHP's json_decode function to get data from the database (phpmyadmin)(the type is set to text) and set it on the page This is the code that i got:
$belangrijkespecs = $productClass->get('belangrijkeSpecs');
$belangrijkespecs = json_decode($belangrijkespecs);
var_dump($belangrijkespecs);
This code outputs
NULL
And when I echo the $productClass->get('belangrijkeSpecs') it outputs:
{"Beeldschermdiagonaal":"10,1 inch (25,7 cm)","Beeldresolutie":"1920 x 1200","Batterijduur":"Tot 12 uur","Gewicht":"525 g","Opslag":"32 GB"}
When I passed this in an online JSON decode website than I get the right array (from the website https://3v4l.org/IHKZZ):
array (
'Beeldschermdiagonaal' => '10,1 inch (25,7 cm)',
'Beeldresolutie' => '1920 x 1200',
'Batterijduur' => 'Tot 12 uur',
'Gewicht' => '525 g',
'Opslag' => '32 GB',
)
When i try json_last_error() with this code:
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
It outputs
- Syntax error, malformed JSON
And if i just use echo json_last_error() it outputs : 4.
i have already tried :
$belangrijkespecs = str_replace("/", "", $belangrijkespecs);
and
$belangrijkespecs = rtrim($belangrijkespecs, "\0");
and
$belangrijkespecs = stripslashes($belangrijkespecs);
the output of var_dump $productClass->get('belangrijkeSpecs') :
string(240) "{"Beeldschermdiagonaal":"10,1 inch (25,7 cm)","Beeldresolutie":"1920 x 1200","Batterijduur":"Tot 12 uur","Gewicht":"525 g","Opslag":"32 GB"
The output of echo addcslashes($belangrijkespecs, '\0..\37!#\177..\377') is:
{"\;\Beeldschermdiagonaal"\;\:"\;\1\0,\1 inch (\2\5,\7 cm)"\;,"\;\Beeldresolutie"\;\:"\;\1\9\2\0 x \1\2\0\0"\;,"\;\Batterijduur"\;\:"\;\Tot \1\2 uur"\;,"\;\Gewicht"\;\:"\;\5\2\5 g"\;,"\;\Opslag"\;\:"\;\3\2 \G\B"\;}
And when i do
$belangrijkespecs = stripslashes($belangrijkespecs); before json_decode it still dosnt work...
Also tried:
$belangrijkespecs = preg_replace('/\\\\/', '', $belangrijkespecs);
But it did not work.
The structure of this 'belangrijkeSpecs' = http://prntscr.com/lvnsbh
I have looked on the internet but the answers that were given did not help me.
So my question is:
How can i get a array from the json encoded string
( $productClass->get('belangrijkeSpecs') )
and I expect an array as a return from the json_decode();
The awnser was that i escaped the string before i did the json_decode. And because i did that there where a lot of backslashes.
Thanks to #Matthijs i found it

I got this error Syntax error, malformed JSON

Im integrating Lightspeed API to my website like POS system. I currently getting the value file_get_contents(), but when Im trying to load it array I gives my the error 4 of json_last_error().
Here is my codes:
$maps_url = "http://cloud-docs.merchantos.com/API/Account/797/Item/?itemID=1";
$maps_json = file_get_contents($maps_url);
//print_r($maps_json);
$maps_array = json_decode(stripslashes($maps_json), true);
echo $data = $maps_array['Item']['systemSku'];
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
json_last_error();
Let me rephrase it then:
The shown URL does not retrieve JSON.
It pulls XML.
Therefore json_decode fails.
Hence there's no usable data in $maps_url.
Which is also why ['Item']['systemSku'] won't work.
(And stripslashes would have made no sense in there anyway).
So, investigate the API you're using. Figure out if there is a JSON variant available. Else throw away your code, and instead use a XML traversal frontend, such as SimpleXML.
As mario has said, $maps_url receives XML.
Check out xml_parse_into_struct for help with XML to array conversion.

JSON syntax error occure when jsone_decode in php but json data actually not contain any syntax error

I am trying to get post details using JSON in wordpress. In my plugin following is the json_decode part. But it returns null.
$post_count = intval($instance['post_count']);
$eng_posts= #json_decode(file_get_contents("http://athavaneng.com/?page_id=206861&posts_per_page=".$post_count), true);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
In the above code I get a JSON_ERROR_SYNTAX error. But actually when I test the returning JSON data of http://athavaneng.com/?page_id=206861 URL on the http://jsonlint.com. It says that "Valid JSON".
Why is that? What is the problem here?
The responding server is sending 3 strange chars before it starts the json output. By doing this:
$var = file_get_contents("http://athavaneng.com/?page_id=206861&posts_per_page=1");
for($i=0; $i<10; $i++) {
echo $var[$i] . " : " . ord($var[$i]) . "<br />";
}
you will get something like this:
ï : 239
» : 187
¿ : 191
{ : 123
" : 34
1 : 49
" : 34
: : 58
{ : 123
" : 34
the first three chars (239,187 and 191) should not been printed by the server. As a workaround you should be able to parse it after stripping the first three chars.
I am able to parse your json with the following code, which beginns reading at character 3:
$var = file_get_contents("http://athavaneng.com/?page_id=206861&posts_per_page=1", false, NULL, 3);
var_export(json_decode($var));

Codeigniter json_decode view as a variable returns NULL

Can't seem to find the solution to this, I want to load json data from a view variable like this
$this->json = $this->load->view('/jsonfiles/page.json','',true);
Outputting
var_dump(json_decode($this->json,true));
= NULL
The View seems to be rendering the .json file incorrectly, for the json_decode to fail. (maybe not incorrectly, but in a different format), Ive already tried to utf8_encode, no luck
JSON Variable within the page.json
[
{
"page" : {
"name" : "PageNameHere",
"date" : true
}
}
]
Or is there an alternative to doing this?
UPDATE: Yes it works when its an actual string in the variable like this
$this->json = '[{"page":{"name":"PageNameHere","date":true}}]';
var_dump(json_decode($this->json,true));
SOLUTION: So I've decided to figure this out one, and by my mistake, I had a "," comma in the last part of my JSON, which wasn't in this thread, I shorten the JSON for topic, but I found it, using this neat error switch, able to find JSON rendering errors
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
My Issue was labeled as "Syntax error, malformed JSON"

Reading JSON from remote URL in PHP [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have this JSON file:
http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json
and I am trying to decode it in PHP as follows:
$json = file_get_contents('http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json');
$data = json_decode($json);
var_dump($data);
But, the output I am getting is NULL. Am I missing anything?
Using example from PHP.NET json_last_error() I found that your json syntax is not correct:
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
Output:
- Syntax error, malformed JSON
However, I checked your json code in the following website but it says valid:
http://www.freeformatter.com/json-validator.html
http://jsonlint.com/
http://json.parser.online.fr/
http://jsonformatter.curiousconcept.com/
Documentation
json_decode can return NULL as the documentation states.
Returns the value encoded in json in appropriate PHP type. Values
true, false and null (case-insensitive) 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.
Your Problem
json_decode is failing here because of \n inside of id 6
{ "id": 6, "url":
"http://jeewanaryal.com/angryQuiz/eighties/images/betterOffDead.jpg",
"question": "In the movie 'Better Off Dead', what was the \n name of
Lane's younger brother?", "answer": [ { "a": { "text": "Bradger",
"status": 1 } }, { "b": { "text": "Peter", "status": 0 } }, { "c": {
"text": "Frank", "status": 0 } }, { "d": { "text": "Michael",
"status": 0 } } ] }
Solution
I guess your best bet here is to escape them before json_decode
$safe_json = str_replace("\n", "\\n", $json);
Please set the second parameter to true. This will give you an associative array. If the result is still null I'd be interested whether the file_get_contents returned something.
This is just a guess, but as you do not show what $json contains, I am going to assume that file_get_contents returned false, leading to the result you get.
You should check your php.ini file, what is the value for allow_url_fopen? If that is 0 that means that you cannot read a file from an http address so that could be the problem.
Just change the value of allow_url_fopen to 1 if that is the problem.

Categories