Regex messing up json_decode in php - php

When I put this into a json checker, it's a valid json, but the json_decode in php gives a decode error. json partial:
"regex":{
"validator":"Regex",
"options":{
"pattern":"\/^[a-zA-Z\\.\\- ]+$\/",
"messages":"Please use letters, spaces, period and dashes only"
}
}
I looked at Regular expression messing up json_decode(); but that didn't help me.
Thanks!
Here is the entire json:

This works.
<?php
error_reporting(E_ALL);
$json = '{"regex":{
"validator":"Regex",
"options":{
"pattern":"\\/^[a-zA-Z\\\\.\\\\- ]+$\\/",
"messages":"Please use letters, spaces, period and dashes only"
}
}
}';
var_dump(json_decode($json, true));
?>
Notice the entire JSON string was encapsulated with {} and also notice all backslashes were escaped with another backslash (so where regex wants \ we have \\). This works perfect.
NOTE UPDATE:
Just str_replace("\\", "\\\\", $json); and you will be fine. Also, if this is submitted in a form it SHOULD be fine. I just submitted your entire JSON string through an HTML form and sent it directly to json_decode (without escaping) and it worked. This is because the browser escapes backslashes already. So long we are not defining the string within PHP it will be escaped (atleast backslashes)

Related

Regex how to escape double quotes if needed

I need to convert all big integers to strings in my response json. Currently my regex look like this
preg_replace(
'/:\s*(-?\d{16,})/',
': "$1"',
$content
);
But problem with such regex is that if my response contains another json string then bigints inside it will be wrapped in string too, but without escaping. Is there any way to escape appended quotes in such case? Or maybe fix incorrect json with another regex?
Example
{"example_bigint": 3330922503411457761}
will be converted to
{"example_bigint": "3330922503411457761"}
but
{"example_json" : "{\"example_bigint\": 3330922503411457761}"}
will be converted to
{"example_json" : "{\"example_bigint\": "3330922503411457761"}"}
when expected output is
{"example_json" : "{\"example_bigint\": \"3330922503411457761\"}"}
There is a flag in the json_decode function:
$myJson = json_decode($jsonString, flags: JSON_BIGINT_AS_STRING);

preg_match removes escaping from json string

I am trying to parse some json data using json_decode function of php. However, I need to remove certain leading and trailing characters from this long string before decoding. Therefore, I am using preg_match to remove those characters prior to decode. For some reason, preg_match is changing escaping when it encounters following substring (in the middle of the string)
{content: \\\"\\200B\\\"}
After preg_match the above string looks like this:
{content: \\"\200B\\"}
Because of this, json_decode fails.
FYI, the preg_match pattern looks like this:
(?<=remove_these_leading_char)(.*)(?=remove_these_trailing_char)
OK, so here is the additional information based on the questions being asked:
Why triple escaping? fix triple escpaing etc. The answer is that I don't have any control over it. It is not generated by my code.
The original string is not fully json compliant. It has several leading and trailing characters that need to be removed. Therefore I have to use regex. The format of that string is like this:
returnedHTMLdata({json_object},xx);
It looks like this behavior is not limited to preg_match only. Even substr also does this.
It looks like you've got some JSON with padding. To remove the function name and parenthesis, leaving the (unescaped) json object, you can do something like this:
$str = <<<'EOS'
returnedHTMLdata({content: \\\"\\200B\\\", foo: \\\"bar\\\", \"baz\": \\\"fez\\\"},xx);
EOS;
$str = preg_replace('/.+?({.+}).+/','$1', $str);
echo $str;
Output:
{content: \\\"\\200B\\\", foo: \\\"bar\\\", \"baz\": \\\"fez\\\"}
Please note that even if you manage to successfully unescape this string, json_decode requires that keys - e.g. "content" - are enclosed in double quotes, so you will need to modify the JSON string/object before calling that function. Or I guess you could instead use something like the old Services_JSON package to decode it, which I believe does not have that requirement.

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?

Javascript string literal error

How would I maintain this code so that it doesn't break on a new line. As a result of this string being broken, I am getting a javascript string literal error.
I need to do this on the PHP side.
I've tried str_replace but that doesn't do it. I don't understand why the line is being broken in the first place.
CKEDITOR.instances.article.setData('<p>Test</p>
');
PHP:
$text = '<p>Test</p>';
Javascript string literals cannot have unescaped newlines.
You need to call json_encode() before printing the string to escape newlines and other special characters.

Can anyone explain this PHP code using json_encode and json_decode?

$a = '{ "tag": "<b></b>" }';
echo json_encode( json_decode($a) );
This outputs:
{"tag":"<b><\/b>"}
when you would think it would output exactly the input. For some reason json_encode adds an extra slash.
Because it's part of the JSON standard
http://json.org/
char
any-Unicode-character-
except-"-or-\-or-
control-character
\"
\\
\/ <---- see here?
\b
\f
\n
\r
\t
\u four-hex-digits
use this:
echo json_encode($a,JSON_HEX_TAG)
Result will be:
["\u003C\u003E"]
You can read this article to improve your knowledge about JSON_ENCODE
http://php.net/manual/en/function.json-encode.php
That's probably a security-feature. The escaped version (Eg. the output) would be parsed as similar to the unescaped-version, by Javascript (Eg. \/ becomes /). Having escaped the slash like that, there is a lesser chance of the browser misinterpreting the Javascript-string as HTML. Of course, if you treat the data correct, this shouldn't be needed, so it's more a safeguard against a clueless programmer messing things up for himself.
Your input is not valid JSON, but PHP's JSON parser (like most JSON parsers) will parse it anyway.

Categories