I am writing some php code which which will be following the oauth2.0 specification. One of the requirements they specify is that any request in which parameters are repeated results in an error. As such, I have a json which I am parsing using json_decode, and I am trying to figure out how to catch if the json repeats any parameters. The result from json_decode seems to just use the last value for the key in the case of repeated parameters, so it seems like I would need to detect them before decoding. Does anyone know how to do this without writing my own json parser?
Thanks!
The approach that I ended up using was to json_encode the json decoded version of the input string and compare it to the input string. If the two match, there were no repeats. If they don't the json_decode saw the repeat, and removed it automatically, and hence there were repeats.
Related
We develop rest api for our service. And now we need to implement response in JSON format.
We get data from db, and encode it to JSON. But some time value return in different type. For example:
"category_id":"12" or "category_id":12
So sometimes it returns as string and sometimes it returns as numeric.
And an other situation sometimes we get response as an array, and sometimes as associative array.
Is it possible some how to set strong type for the value? Maybe you recommend some third party tools.
If you want to treat all numeric value as number and return data type number then you have to use JSON_NUMERIC_CHECK option.
like following:
json_encode($arrays, JSON_NUMERIC_CHECK);
I am generating a sequential string of independent JSON strings for insertion into a single field in my database (the means justify the end), and am wondering if a JSON string could ever legally have an occurrence of two opposing(ly) faced curly braces such as }{? As I would like to use this pattern as a delimiter if so.
I am using PHP's json_encode function for this purpose.
Should say that I don't mean as a value - or key if that were possible as I am in control of the data. Seems like a stupid question now.
Yes. It can form part of a string in JSON text.
{
"EskimoKiss": "}{"
}
If you must store multiple pieces of data expressed as JSON in a database field, then parse them to objects, wrap them in an array, then serialise that array to JSON and store that.
You really should normalise the data though.
I am using json_encode to transform my php multidimensional array to output json. Normally, this function would convert all values to strings. To make sure that integers values are send to javascript as integer values, I am using the numeric check:
$json = json_encode($data, JSON_NUMERIC_CHECK);
This works fine in all but one case for my app. In the php array (which is extracted from the database), there is one field that contains very large integers. I save it to database as a VARCHAR, but unfortunately this is converted to an integer when encoding to json. The problem is that since this is a very large integer, it gets rounded and therefore does not represent the true value. How could I tackle this problem?
Do you want the large number to be transformed to an integer? Your question leads me to believe you don't. If that's the case, remove the JSON_NUMERIC_CHECK option from the call and it shouldn't change the encoding of the field.
Documentation about this (and other) constants is here.
Maybe is to late but i did hit the same problem, and stuck on PHP 5.3 on the server because of legacy code that must be run with that version. The solution that i used is dumb, but did work for me: simple add an space character at the end of the long integer that is varchar readed from the db, and before sending it to json encode with JSON_NUMERIC_CHECK.
I've been on working on this for ages. I am receiving a JSON from a request to an API and unfortunately the key's of each entry is a time uuid which is in binary format. I'm trying to use json_decode() to decode the json into a PHP array. However, when I echo the string I have about 80 entries but after the decode into the array, it trims it down to about 40 entries,
I should mention that I don't need these values that are in the keys, I could just strip them off if possible using array_values() maybe?
Any advice would help Thanks!
Here's some sample data
"\u001c":{"down":"1280069835000","off":"1279893600000","on":"1279886400000","up":"1280077035000"},"=":{"down":"1280163435000","off":"1279893600000","on":"1279886400000","up":"1280167035000"}
I actually contacted the development team, and they converted the binary uuid to string format, and it worked like a charm. Looks like this sort of thing may not be possible besides manually parsing out the JSON. Thanks for your efforts all!
The Interwebs are no help on this one. We're encoding data in ColdFusion using serializeJSON and trying to decode it in PHP using json_decode. Most of the time, this is working fine, but in some cases, json_decode returns NULL. We've looked for the obvious culprits, but serializeJSON seems to be formatting things as expected. What else could be the problem?
UPDATE: A couple of people (wisely) asked me to post the output that is causing the problem. I would, except we just discovered that the result set is all of our data (listing information for 2300+ rental properties for a total of 565,135 ASCII characters)! That could be a problem, though I didn't see anything in the PHP docs about a max size for the string. What would be the limiting factor there? RAM?
UPDATE II: It looks like the problem was that a couple of our users had copied and pasted Microsoft Word text with "smart" quotes. Those pesky users...
You could try operating in UTF-8 and also letting PHP know that fact.
I had an issue with PHP's json_decode not being able to decode a UTF-8 JSON string (with some "weird" characters other than the curly quotes that you have). My solution was to hint PHP that I was working in UTF-8 mode by inserting a Content-Type meta tag in the HTML page that was doing the submit to the PHP. That way the content type of the submitted data, which is the JSON string, would also be UTF-8:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
After that, PHP's json_decode was able to properly decode the string.
can you replicate this issue reliably? and if so can you post sample data that returns null? i'm sure you know this, but for informational sake for others stumbling on this who may not, RFC 4627 describes JSON, and it's a common mistake to assume valid javascript is valid JSON. it's better to think of JSON as a subset of javascript.
in response to the edit:
i'd suggest checking to make sure your information is being populated in your PHP script (before it's being passed off to json_decode), and also validating that information (especially if you can reliably reproduce the error). you can try an online validator for convenience. based on the very limited information it sounds like perhaps it's timing out and not grabbing all the data? is there a need for such a large dataset?
I had this exact problem and it turns out it was due to ColdFusion putting none printable characters into the JSON packets (these characters did actually exist in our data) but they can't go into JSON.
Two questions on this site fixed this problem for me, although I went for the PHP solution rather than the ColdFusion solution as I felt it was the more elegant of the two.
PHP solution
Fix the string before you pass it to json_decode()
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
ColdFusion solution
Use the cleanXmlString() function in that SO question after using serializeJSON()
You could try parsing it with another parser, and looking for an error -- I know Python's JSON parsers are very high quality. If you have Python installed it's easy enough to run the text through demjson's syntax checker. If it's a very large dataset you can use my library jsonlib -- memory use will be higher than with demjson, but it will run faster because it's written in C.