json_encode remove backslash - php

I get json encoded array from DB change one field and save again but json_encode remove the \ and after I cant see the text in my site.
$data_de=json_decode($row["data_json"], true);
$data_de[$ref."_visits"]++;
$datast=json_encode($data_de);
for example if before code the value on db was:
{"7_id":"7","7_name":"\u05d1\u05d3\u05d94","7_coded":"","7_visits":"0"}
after the value:
{"7_id":"7","7_name":"u05d1u05d3u05d94","7_coded":"","7_visits":"1"}
how I can prevent the removing of backslash?

To prevent removing of backslashes use this code in your json encode:
json_encode($data_de, JSON_UNESCAPED_SLASHES);

Related

How to remove escaping in PHP?

I have this URL parameter:
KKe%7bZoE_%24g)tjm%40
When I put it into a variable and echo it, the result is:
KKe{ZoE_$g)tjm#
How to avoid that?
Data in $_GET is already URL-decoded. If you require the original string, get it from $_SERVER['QUERY_STRING']. Note that you will have to process the query string yourself though, including breaking down the individual components.
Alternatively, use rawurlencode($_GET[..]) to re-encode the value; which may or may not produce slightly differently encoded values than you originally got.
Test it with html_entity_decode - it helpt me a lot with my inputs.
If the string is not shown as it is, you have urlencode() or htmlentities() somewhere in your code. Check that, you shouldn't encode html entities before echoing if you want the string to be intact.

PHP json_decode a json_encoded string

I don't really get the following...
// array to encode
$a = ['regex' => '\/\+\/'];
// decoding an encoded array works
print_r(json_decode(json_encode($a), true));
// encoded array
echo json_encode($a);
// trying to decode the before encoded string doesn't work
print_r(json_decode('{"regex":"\\\/\\+\\\/"}', true));
echo json_last_error_msg();
The last error message says Syntax error. Shouldn't I be able to easily decode a simple encoded string?
I know that the problem is in the backslashes but I won't want to do any magic string replacement or regex to get the decoding working. Just want to understand where goes what wrong and what's a best practice for this kind of situations?
I'm using PHP version 5.5.9
Found something thanks to that answer : https://stackoverflow.com/a/10929508/1685538
If instead of taking the string outputted by echo, you use var_export(json_encode($a)); (documentation), it gives you {"regex":"\\\\\\/\\\\+\\\\\\/"}
print_r(json_decode('{"regex":"\\\\\\/\\\\+\\\\\\/"}', true)); gives the expected result :
Array
(
[regex] => \/\+\/
)
with no errors.
The problem is that using backslashes in PHP code string literals is subject to PHP's backslash escaping rules. You need to additionally escape the backslashes so they are preserved inside the PHP string:
print_r(json_decode('{"regex":"\\\\\\/\\\\+\\\\\\/"}', true));
Contrast with:
echo '{"regex":"\\\/\\+\\\/"}';
// {"regex":"\\/\+\\/"}

json decode of url request which contain special character

I have a url request like this:
http://localhost/pro/api/index/update_profile?data={"id":"51","name":"abc","address":"stffu fsagu asfhgui fsahgiu3#$#^^##%^3 6\"\"wkgforqf\";rqgjrg..,,,rqwgtr''qwrgtrw'trwqt'rqwtqwr trqt\n"}
I am trying to json decode of this url.I use following code to decode url.It is working perfect if url not contain special character. but how to decode it if it contains special character.
$string = htmlspecialchars($_REQUEST['data'], ENT_QUOTES);
$jsonFix = urldecode($string);
$string = htmlentities($jsonFix, ENT_QUOTES | ENT_IGNORE, "UTF-8");
$json = json_decode($string, true);
print_r($json);exit;
I tried this code but it is not working.when i am try following:
print_r($_REQUEST['data']);exit;
output is:
{"id":"51","name":"ds"","address":"stffu fsagu asfhgui fsahgiu3
means it is bracking from # character.
(sidenote: i am working on api for iphone so request came from iphone,framework:CI)
so how to get url which contain special character and how to decode it?
The # character marks the beginning of the fragment part of the URL.
You need to properly URL-encode the URL for this to work.
For example, your JSON, when correctly URL-encoded, becomes:
%7B%22id%22%3A%2251%22%2C%22name%22%3A%22abc%22%2C%22address%22%3A%22stffu%20fsagu%20asfhgui%20fsahgiu3%23%24%40%5E%5E%40%23%25%5E3%206%5C%22%5C%22wkgforqf%5C%22%3Brqgjrg..%2C%2C%2Crqwgtr%27%27qwrgtrw%27trwqt%27rqwtqwr%20trqt%5Cn%22%7D
The entire URL becomes:
http://localhost/pro/api/index/update_profile?data=%7B%22id%22%3A%2251%22%2C%22name%22%3A%22abc%22%2C%22address%22%3A%22stffu%20fsagu%20asfhgui%20fsahgiu3%23%24%40%5E%5E%40%23%25%5E3%206%5C%22%5C%22wkgforqf%5C%22%3Brqgjrg..%2C%2C%2Crqwgtr%27%27qwrgtrw%27trwqt%27rqwtqwr%20trqt%5Cn%22%7D
Check the documentation of your language of choice to find the correct method for URL-encoding characters.
For example, in PHP, this is rawurlencode and in JavaScript this is encodeURIComponent.
If necessary, there are also plenty of URL coders online, such as this website.
You are manipulating the $data in some ways that aren't really necessary. htmlspecialchars() and htmlentities() make sense if applied to specific values - not the whole JSON. The danger is that they mess up the JSON, it is only important here to urldecode()!
$jsonFix = urldecode($data);
$json = json_decode($jsonFix, true);
This already works and doesn't leave any character out.
If you plan to post something of that and want to escape it, you can do it like so
htmlspecialchars($json['address'], ENT_QUOTES)
Can't you just replace the "#" character with something like "&hashtagChar;" before you process, and put it back afterwards?

JSON Quotes Being Unescaped In POST

I have a piece of code that accepts a JSON string as a POST parameter. The challenge I'm having is how the string is decoded. Consider a post to mygateway.php with a field meta of:
{"test" : "One \"quote\" is as good as an escaped \"quote\"..."}
If I run:
$meta_json_string = $this->CI->post('meta', true);
The value of $meta_json_string is:
{
"test": "One "quote" is as good as an escaped "quote"..."
}
This fails to decode when run through json_decode(). Any suggestions on how to prevent CodeIgniter from unescaping the quotes so I can decode it?
Change it to this:
$meta_json_string = $this->CI->post('meta');
This way CI will not be running the XSS sanitation on the string which is too complex to reliably sanitize being a JSON string. Once you do the above then json_decode() it and then run $this->security->xss_clean() individually on the elements you are wanting to echo out.

How can I emulate php's json_encode in javascript / node js using JSON.stringify?

I'm looking for a way to mimic php's json_encode's behavior from node js. Here's an example showing what php does with a url that is in an object that gets json_encoded:
<?
$foo['url'] = "http://example.com/note/that/the/slashes/get/backslashed";
echo json_encode($foo);
?>
generates the following output:
{"url":"http:\/\/example.com\/note\/that\/the\/slashes\/get\/backslashed"}
Using node.js and the JSON.stringify function here:
var foo = new Object();
foo.url = "http://example.com/note/that/the/slashes/do/not/get/backslashed"
console.log(JSON.stringify(foo));
I observe this output instead:
{"url":"http://example.com/note/that/the/slashes/do/not/get/backslashed"}
Do you know of a clean way to get JSON.stringify to behave the same way that PHP behaves?
Extra information: I realize that these slashes may not be required for correct json encoding but I am sending json encoded objects to a remote server that I don't have control over and doesn't like them without the backslashes.
More extra information: And I tried putting in my own backslashes and then calling JSON.stringify but JSON.stringify dutifully does properly escape the backslashes so I ended up with \\/ instead of \/ which is what I wanted.
If it's only the slashes you can replace the / with \/ after the conversion.

Categories