JSON_EXTRACT not working for nested json data - mysql

I want to select the data from my table which is json data so to show my table data is like this :
user_id: 1
metaname: mymetaname
meta_value: a:1:{i:0;a:10:{s:7:"street1";s:36:"shiv plaza";s:4:"city";s:5:"surat";s:5:"state";s:7:"gujarat";s:7:"zipcode";s:6:"395010";s:14:"dollet_country";s:2:"IN";s:10:"tostreet1l";s:5:"surat";s:7:"tocityl";s:5:"surat";s:8:"tostatel";s:5:"surat";s:10:"tozipcodel";s:6:"395000";s:17:"todollet_countryl";s:2:"IN";}}
And i am trying to run this query :
SELECT user_id,JSON_EXTRACT(meta_value, '$."city"') FROM `usermetatable`
But it's showing error :
[Invalid JSON text in argument 1 to function json_extract: "Invalid
value." at position 0.]
My json data in table can not be changed to other and it's correct JSON for sure, Could anyone correct above query ?

That's not JSON data. It looks like a serialized PHP object. See http://php.net/serialize
There's no MySQL function for extracting a field from that serialized object. You should fetch the whole object into a PHP app, and call unserialize() on it, then access the object members.

Related

How to parse serialized json in Postgresql?

I have a data like below and I would like to get a value of sub_key1:
'{"key_1":"val_1", "key_2":"{\"sub_key1\":\"sub_val1\", \"sub_key2\":\"sub_val2\"}"}'
If I run below query, it works fine and gets me the value of key_2.
SELECT ('{"key_1":"val_1", "key_2":"{\"sub_key1\":\"sub_val1\", \"sub_key2\":\"sub_val2\"}"}')::json->'key_2';
But if I run below query, I do not get anything in return.
SELECT (('{"key_1":"val_1", "key_2":"{\"sub_key1\":\"sub_val1\", \"sub_key2\":\"sub_val2\"}"}')::json->'key_2')::json->'sub_key1';
How to get a value of sub_key1?
The value you get out by using -> is a JSON string literal. Casting that to json will do nothing, and accessing a property on a string doesn't work.
You'll need to use ->> instead to get the string as a postgres text which you then can convert to a json object:
SELECT (('{"key_1":"val_1", "key_2":"{\"sub_key1\":\"sub_val1\", \"sub_key2\":\"sub_val2\"}"}')::json->>'key_2')::json->'sub_key1';
But either way, fix the system that generates this JSON not to put serialised JSON strings into JSON.

How to select from MySQL where colmun is type text and data is json

Magento 2 table 'sales_order_payment' have column 'additional_information' which is in data type 'text'. Now data inside this field I see that is json (but as string - text)
{"raw_details_info":{"CustomerFirstname":"Mary",...
How can I select this as json?
I have try this. First I create view where I cast this column as json:
CREATE VIEW JSONTEST as SELECT cast(additional_information as json) as test FROM sales_order_payment;
Then I try to select this as json:
SELECT test->>'$.CustomerFirstname' from JSONTEST
But result is null. Any idea what is wrong?
Your path must start from the top of the document.
SELECT test->>'$.raw_details_info.CustomerFirstname' ...
Also the JSON stored in your TEXT column must be valid JSON, or else your cast will return an error. This is an advantage of the JSON data type in MySQL 5.7 and later, because it will require the content to be valid JSON.

snowflake read null from s3 json

This seems like it should be easy, but for the life of me on a Friday night...
I have a json file i'm reading from s3
{"name":"bob", "currentTime":"null"}
I created a stage in snowflake.
When I do,
Select $1:name, $2:currentTime
from #myStage/mydocument
I get as expected
$1:name $2:currentTime
"bob" "null"
I have a snowflake table
create table test_bob
(
name varchar
,currentTime TIMESTAMP_NTZ
)
But when I do
Copy into test_bob
Select $1:name, $2:currentTime
from #myStage/mydocument
I get an error,
Failed to cast variant value "null" to TIMESTAMP_NTZ
I tried using
NULL_IF
as suggested here.
I tried using STRIP_NULL_VALUES as a file format
It looks like you have a string of "null" rather than a null value. Did you try this?
Copy into test_bob
Select $1:name, NULLIF($2:currentTime::string,'null')::timestamp_ntz
from #myStage/mydocument
this should check the value of the string before trying to convert the json attribute object to a timestamp_ntz.

PostgreSQL JSON query returns NULL

I've imported a raw data export from Unity into PostgreSQL using the JSON file that Unity offers.
Sample of data:
{"name":"EVENT1","ts":1534117312648,"userid":"1e77723b38980460ea307db5fca875fd","sessionid":"3188654687037448331","platform":"AndroidPlayer","sdk_ver":"u2017.4.1f1","debug_device":false,"user_agent":"Dalvik/2.1.0 (Linux; U; Android 7.0; LG-H820 Build/NRD90U)","submit_time":1534118874283,"custom_params":{"the Daily Bonus":"RewardGems"},"country":"US","city":"Fayetteville","appid":"50d97d88-096c-4a4b-8daa-390e239974f8","type":"custom"}
{"name":"GAME1","ts":1534107814910,"userid":"f029c3982539e4eeea171132bd9cf8c9","sessionid":"220388644753439310","platform":"AndroidPlayer","sdk_ver":"u2017.4.1f1","debug_device":false,"user_agent":"Dalvik/2.1.0 (Linux; U; Android 7.0; SM-G570M Build/NRD90M)","submit_time":1534118931705,"custom_params":{"Heavy Slam":"1","Flame Breath":"1","Cure":"1","Chop":"1","Bite":"7","Fang Fireball":"10","Axe Throw":"2"},"country":"BR","city":"Várzea Grande","appid":"50d97d88-096c-4a4b-8daa-390e239974f8","type":"custom"}
The JSON data is in the values column in the temp_json table as a text data type. When trying to CAST the column as a JSON datatype I get the following error:
ERROR: invalid input syntax for type json
DETAIL: Character with value 0x0a must be escaped.
CONTEXT: JSON data, line 1: ...tric Jacket":"1","Bolt":"5","Axe Throw":"1","Toss
SQL state: 22P02
As a result I've had to use to_json(values)to convert the text column into JSON. When I attempt to run the following query:
SELECT to_json(values) -> 'name'
FROM temp_json
My query results in NULL. I've searched around and found an answer that stated to try the following query:
select json_array_elements(to_json(values)) ->> 'name'
from temp_json
Although that results in the following error:
ERROR: cannot call json_array_elements on a scalar
SQL state: 22023
I'm extremely new to JSON and PostgreSQL so apologies for the noob question. Any help would be very much appreciated. I feel like this should be an easy thing to figure out, but I can't seem to find a solution.
If you are trying to return the "name" parameter from the JSON string you could use the JSON_EXTRACT_PATH_TEXT function. See docs:
https://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_PATH_TEXT.html
Here is an example of how to use it assuming the JSON string is in the values column in the temp_json table:
SELECT JSON_EXTRACT_PATH_TEXT(values, 'name') AS name
FROM temp_json;
Hope this helps!

Convert from MySQL query result or LIST to JSON

I am using anorm with play-scala and have execute queries using:
val result = SQL("SELECT * FROM users)()
What I have been doing is the following:
val queryResult = SQL(query)().map(result =>
result.asList
).toList
However, I have realized that instead of converting it to a list, I want to actually receive JSON. Since I may change the query, I would like the ability for a JSON serializer that takes in any result and understands the column names and convert them and the results into JSON.
EDIT:
I'm using play 2.3.10 with scala version 2.11.6.
I'm trying to map the SqlResult's metadata column names to an object or rather a json parser. I was able to get the SqlResult's metadata as
List(MetaDataItem(ColumnName(x),false, java.lang.Integer), MetaDataItem(ColumnName(y),.....
Thanks