json encode in php - php

I am storing this {background:"default.jpg"} in the database in field of tables as i am taking the table fields data and makint the json by json_encode while encoding this json also get encoded and it is not valid json so how should i encode these json.
{"id_session":"c72b0581e7675b596a7651a7bb906438","gibid":"54","name":"Market Place","type":"S","num_owners":"0","inner_template":"","inner_data":"{background:\"default.jpg\"}","outer_template":"","o
it is adding the slashes how should i get return the valid json.
thanks.

Don't store JSON in the database, store it in a neutral format, like key/value columns. Or:
Decode the JSON, merge it into the array to be encoded, then encode it.
$data = array('id' => ...);
$data['inner_data'] = json_decode($databaseJson, true);
json_encode($data);

So you have a JSON string in the database and then you get it out of it and do a json_ecnode on ir again? For php you are just encoding some string (thats why the backslashes on the quotations marks are coming from).
But to achieve what you actually want, you could to decode the JSON string from the database first and then encode it with the rest of your data again.

Related

Extra slashes on json response

On database: { "email", "color" }
But when I try to output it using application/json' => \yii\web\Response::FORMAT_JSON, the string contains extra slashes
[
"Verify",
"{ \"email\", \"color\" }"
]
I know that I can sort of use the replace() but can someone enlighten me on this scenario?
the encode add the slashes for prevent improper quotes sequence that break the corret use of content
whitout slashes you have in yoru case
"{ "email", "color" }" // that is not correctly formateed
you have already quote around you value in database.. and you data are already in json format so so you could use without encode
#webDav almost has the correct answer.
It appears you are incorporating the data from the database into another data structure. So you will need to parse the JSON from the database first, then include that data in your data before encoding again.
You are not retrieving raw string (similar to JSON) from the DB, but it is a string that needs to be converted to JSON in order to have meaning.
It seems to me that you are trying to encode an already json encoded data.
As a results it escapes the characters.
You have 2 options:
Do not format it to JSON again
Decode the data with json_decode() before return
If you need to append or combine that json data, then #2 is your only option

Saving utf-8 array in database laravel

I have problem with saving utf-8 array in database with laravel.
when i save array in database it is stored like this
\u10e4\u10dd\u10dd\u10d3
when i display data it works fine, but when i run search query it displays nothing.
database encoding is utf-8 with general_ci collation.
i've already tried to encode data before saving in database
json_encode($data, JSON_UNESCAPED_UNICODE);
any ideas?
Try storing these arrays in your database using the PHP serialize function instead of encoding for JSON.
serialize($data);
When you want to use the data back, simply deserialize the array with PHP unserialize function.
unserialize($data);

How to recieve a json encoded string POSTED and extract them into variables php

how will I accept a json data
{
"ccToken": "75676576765",
"cardType": "CC",
}
which will be posted in my url and store the values in variables,like
$ccToken="756765676765"
cardType="CC"
I may sound dump..Im just new to this,so I dont know how I will accept a json string posted on a url..Usually if its variables,I can accept it like,
$ccToken = $_POST['ccToken'];
but for this thing.
and how will be able to sent a response like..200 in return?
Assuming your entire POST body is a JSON string, you'll need to pull in and decode the entire JSON from the body:
$raw_post_body = file_get_contents('php://input');
$data_object = json_decode($raw_post_body);
Then you can access the json decoded $data_object like:
echo $data_object->ccToken;
use the javascript JSON.stringify() method
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
and the php json_decode() function
http://uk3.php.net/json_decode

decode json format data sent via url

consider the following url
index.php?json={uid:guest|10001441}
I am not able to decode the data .
I need the output as {"uid":"guest|10001441"}.
The code i m using is
if (isset($_GET['json']))
{
$url_data = $_GET['json'];
$decoded_data = json_decode($url_data,true);
var_dump($decoded_data);
}
But it gives me output as NULL. What am i doing wrong?? Do i need to pass data in a different format??
It seems impossible to decode a JSON string without double quotes, as it's hard to determine whether a JSON string "{A:B:C}" is encoded from {"A":"B:C"} or from {"A:B":"C"}
You can use urlencode() to encode the JSON string or just ignore the extreme cases and add the double quotes manually to make the JSON string valid, hope this help :D
The data you're passing is not valid JSON. It doesn't have double quotes around it currently. Try this:
if (isset($_GET['json']))
{
$url_data = '"'.$_GET['json'].'"';
$decoded_data = json_decode($url_data,true);
var_dump($decoded_data);
}
Output:
string(20) "{uid:guest|10001441}"
Also, I'd suggest using POST instead of GET here.
If you pass it like this it will work: ?json={"uid":"guest|10001441"}
But I am not sure whether it is a proper method.
The JSON you have in your URL is not valid. PHP's json_decode looks for double quotes around the uid and it's value.
EDIT: Rather than storing the data in the URL as json could you not store the data as key value pairs such as
?uid=value
And then you could do something like
$data = json_encode( $_GET );
you can encode url parameter with base64_encode()
$json=base64_encode('{"uid":"guest|10001441"}');
url will look like this
index.php?json=eyJ1aWQiOiJndWVzdHwxMDAwMTQ0MSJ9
then do like this :-
if (isset($_GET['json']))
{
$url_data = base64_decode($_GET['json']);
$decoded_data = json_decode($url_data,true);
var_dump($decoded_data);
}

How to convert URL encoded JSON array into PHP array

how can convert URL encoded JSON sent from my android application into PHP array.Current format i am getting looks like below format.
[{\\\"product_id\\\":\\\"33\\\",\\\"amount\\\":\\\"1\\\"},{\\\"product_id\\\":\\\"34\\\",\\\"amount\\\":\\\"3\\\"},{\\\"product_id\\\":\\\"10\\\",\\\"amount\\\":\\\"1\\\"}]
How can i convert those data into below format
product_id amount
33 1
34 3
10 1
Because i want to insert those data into MySQL database.Can anyone please help me regarding this problem.
Use json_decode :
echo json_decode($jsonarr);
this can be done by json_decode
$json = '{"foo": 12345}';
$obj = json_decode($json);
print $obj->{'foo'}; // 12345
Good read
--- common mistakes using json_decode()
json_decode(jsonencoded string, TRUE) is correct way to encode json string.
But your json string encoded too deeper and recursive. your json encode string should as below
[{"product_id":9,"amount":500},{"product_id":9,"amount":500},{"product_id":9,"amount":500}]
Other wise, you will get NULL value

Categories