I Have a use case, where I generate a hash of a JSON object created in java initially and insert the hash and JSON object into the Postgres table as String and jsonb respectively.
I need to validate the JSON object saved initially in regular intervals, During that I fetch the JSON object from Postgres which is stored in jsonb and generate a hash out of it and compare it with the hash generated initially. Both are different now.
The reason is initially when data was inserted order of parameters was different in JSON Object at retrieval the order is different. Ending up generating 2 different hash for the same data.
Please suggest.
Use json type instead of jsonb per JSON:
Because the json type stores an exact copy of the input text, it will preserve semantically-insignificant white space between tokens, as well as the order of keys within JSON objects. Also, if a JSON object within the value contains the same key more than once, all the key/value pairs are kept. (The processing functions consider the last value as the operative one.) By contrast, jsonb does not preserve white space, does not preserve the order of object keys, and does not keep duplicate object keys. If duplicate keys are specified in the input, only the last value is kept.
Related
My input file is line-delimited JSON objects (one object per line). Not every key is guaranteed to exist in each and every object.
After reading in the input file, how can I convert it to CSV where the header is the combined list of all possible keys. If a particular object doesn't have a key-value, that column is just empty.
I have a little dilema. I have a backend/Frontend Application that comunicates with a JSON based REST Api.
The backend is written in PHP(Symfony/jmsserializer) and the Frontend in Dart
The communication between these two has a little Problem.
For most List Data the backend responds with a JSON like
[{"id":1,...},{"id:2,....}]
But for some it responds with
{"0":{"id":1,...}, "1":{"id:2,....}}
Now my Question is should the backend respond with the later at all or only with the first?
Problem
You usually have a list of objects. You sometimes get an object with sub-objects as properties.
Underlying issue
JS/JSON-Lists are ordered from 0 upwards which means that if you have PHP-Array which does not respect this rule json_encode will output a JS/JSON-Object instead using the numeric indices as keys.
PHP-Arrays are ordered maps which have more features that the JSON-Lists. Whenever you're using those extra features you won't be able to translate directly into JSON-Lists without loosing some information (ordering, keys, skipped indices, etc.).
PHP-Arrays and JSON-Objects on the other hand are more ore less equivalent in terms of features and can be correctly translated between each other without any loss of information.
Occurence
This happens if you have an initial PHP-Array of values which respects the JS/JSON-List rules but the keys in the list of objects are modified somehow. For example if you have a custom indexing order {"3":{}, "0":{}, "1":{}, "2":{}} or if you have (any) keys that are strings (ie. not numeric).
This always happens if you want to use the numeric id of the object as the numeric index of the list {"123":{"id": 123, "name": "obj"}} even if the numeric ids are in ascending order... so long as they are not starting from 0 upwards it's not a json-list it's a json-object.
Specific case
So my guess is that the PHP code in the backend is doing something like fetching a list of objects but its modifying something about it like inserting them by (string) keys into the array, inserting them in a specific order, removing some of them.
Resolution
The backend can easily fix this by using array_values($listOfObjects) before using json_encode which will reindex the entire list by numeric indices of ascending value.
Arrays and dictionaries are two separate types in JSON ("array" and "object" respectively), but PHP combines this functionality in a single array type.
PHP's json_encode deals with this as follows: an array that only contains numeric keys ($array = ['cat', 'dog']) is serialized as JSON array, an associative array that contains non-numeric keys ($array = ['cat' => 'meow', 'dog' => 'woof']) is serialized as JSON object, including the array's keys in the output.
If you end up with an associative array in PHP, but want to serialize it as a plain array in JSON, just use this to convert it to a numerical array before JSON encoding it: $array = array_values($array);
I'm using built-in functionality to create JSON string in Flash app.
Here example of my source code
objStr = JSON.stringify(
{
version:"1.0",
skin:"white",
palette:{dataColor:"#0397d6",negativeDataColor:"#d40000",toolbarColor:"#056393"}
});
I have a problem. Every time I've started my app (not executing createJSON function), I have different member order in JSON string as result.
For example:
{"version":"1.0","palette":{"negativeDataColor":"#d40000","dataColor":"#0397d6","toolbarColor":"#056393"},"skin":"white"}
or
{"palette":{"negativeDataColor":"#d40000","toolbarColor":"#056393","dataColor":"#0397d6"},"version":"1.0","skin":"white"}
How can I fix it.
JSON objects are unordered, see JSON.org:
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is
realized as an object, record, struct, dictionary, hash table, keyed
list, or associative array. An object is an unordered set of name/value pairs
An ordered list of values. In most languages, this is realized as an
array, vector, list, or sequence. An array is an ordered collection of values.
Order really doesn't matter since you should be retrieving the values by the key rather than iterating over them.
Are there any good UDFs in MySQL to deal with json data, that supports the ability to retrieve a particular value in json (by dot notation key - EG: json_get('foo.bar.baz')) as well as the ability to set the value of a particular key - EG: json_set('foo.bar.baz', 'value')?
I found http://www.mysqludf.org/lib_mysqludf_json/ - but it seems to only provide the ability to create json data structures from non-json column values, as opposed to interacting with json column values.
This UDF is able to parse JSON and return the value of an attribute:
https://github.com/kazuho/mysql_json
This other one too: https://github.com/webaroo/mysql-json-udf
I am doing some processing with Groovy/Grails and the results are a map of type <String, Float>.
When returning the JSON object to the calling function (in this case, it's a flot diagram which requires [number,number] format), the key needs to be a number. This, in theory, is fine as my key to the map is a number in string form. I can't, however, figure out a way to create the JSON object with a numerical key.
I get results like this:
{"1":-9.814244910221474,"2":-9.710478606504552,"3":-9.636841089398253,"4":-9.524104819110796,"5":-9.522597036735684 ...}
instead of:
{1:-9.814244910221474,2:-9.710478606504552,3:-9.636841089398253,4:-9.524104819110796,5:-9.522597036735684 ...}
Does anyone know a way to force the JSON Map.encodeAsJSON() to produce an integer key? I've tried explicitly creating a map of type < integer,integer > before encoding it, and that doesn't work either.
Mike, Im looking at the json spec -- it appears that the keys must be strings. You should handle this client side.
http://www.json.org/