I want to extract the new status value from the below JSON
{"old_status":"Testing(PhaseI)","new_status":"Production(PhaseII)","user":"Developer","reason":null}
so the output required is Production(PhaseII), pls note that the new_status's values are completely dynamic , how to do this using JSON_EXTRACT()
SELECT JSON_UNQUOTE(JSON_EXTRACT(`field``, '$.new_status'))
Related
I have the following value as String in one column in bigquery table. I need to extract the content value using BigQuery view.
{type:System.Int32,content:202104}
I have converted the string like below and tried the JSON_EXTRACT syntax its working.
SELECT
JSON_EXTRACT(JSON'{"type":"System.Int32","content":202104}', '$.content')
AS json_data;
But I have converted this manually from
{type:System.Int32,content:202104}
to
{"type":"System.Int32","content":202104}
How can I achieve the same using query? Can anyone help me to resolve this query, Thanks in advance!
You may use REGEXP_EXTRACT to directly extract the value of the "content". Please refer to BigQuery REGEXP_EXTRACT for more information on the usage of this string function.
SELECT REGEXP_EXTRACT('{type:System.Int32,content:202104}', r'content:(\d+)') as CONTENT_EXTRACT
Output:
This may help:
SELECT
JSON_EXTRACT_SCALAR(REGEXP_REPLACE('{type:System.Int32,content:202104}', r'[^\:\,{}]+', r'"\0"'), '$.content')
AS json_data
I am using MySQL 5.7+ with the native JSON data type.
Sample data:
set #jsn_string='{\"body\": {\"items\": \"[{\\\"count\\\":530,\\\"id\\\":5},{\\\"count\\\":1,\\\"id\\\":519},{\\\"count\\\":209,\\\"id\\\":522},{\\\"count\\\":0,\\\"id\\\":3004}] \"}}';
Questions: the correct answer is 530
The following query has the position of the data
select json_extract(#jsn_string,'$.body.items[0].id[0]');
but the result is : null
we can use json_unquote to remove those double quotes in items[0]
set #jsn_string='{\"body\": {\"items\": \"[{\\\"count\\\":530,\\\"id\\\":5},{\\\"count\\\":1,\\\"id\\\":519},{\\\"count\\\":209,\\\"id\\\":522},{\\\"count\\\":0,\\\"id\\\":3004}] \"}}';
select json_extract(json_unquote(json_unquote(json_extract(#jsn_string, '$.body.items[0]')))
,'$[0].count');
see dbfiddle
As per your sample JSON, id is not an array. So id[0] wont work As I understand from your question, you want to retrieve 530. For this you can use the following.
select json_extract(#jsn_string,'$.body.items[0].count');
Please let me know if I understood it incorrectly.
Edit - 1:
I think your json is invalid. Array should not be wrapped in braces. Try this
#jsn_string='{\"body\": {\"items\": [{\\\"count\\\":530,\\\"id\\\":5},{\\\"count\\\":1,\\\"id\\\":519},{\\\"count\\\":209,\\\"id\\\":522},{\\\"count\\\":0,\\\"id\\\":3004}]}}';
How to set value in MySQL(5.6) column if that contains JSON document as a string
For example, if we have a table - user in that we have three columns id, name and jsonConfig and column jsonConfig contains data as a JSON document
{"key1":"val1","key2":"val2","key3":"val3"}
I would like to replace the value of val1 let's say to val4 for jsonConfig column
Can we do that using MySQL(5.6) queries?
I don't thing their is direct way to do this like in later version alot of json support was added like JSON_EXTRACT, JSON_CONTAINS etc.You might have to write your own custom function.
With MySQL 5.6, since it does not have the JSON data type or the supporting functions, you are going to have to replace the entire string via an UPDATE query if you want to change any part of the JSON document in your string.
I have a JSON array in the MySQL payment table details column. I need to update a single value of this JSON array. What is the procedure to update JSON using MySQL?
JSON Array
{"items":[{"ca_id":18,"appointment_date":"2018-09-15 15:00:00","service_name":"Software Installation / Up-gradation","service_price":165}],"coupon":{"code":"GSSPECIAL","discount":"10","deduction":"0.00"},"subtotal":{"price":165,"deposit":0},"tax_in_price":"included","adjustments":[{"reason":"Over-time","amount":"20","tax":"0"}]}
I need to update the appointment _date 2018-09-15 15:00:00 to 2018-09-28 15:00:00.
Here is a pure MySQL JSON way of doing this:
UPDATE yourTable
SET col = JSON_REPLACE(col, '$.items[0].appointment_date', '2018-09-28 15:00:00');
The best I could come up with is to address the first element of the JSON array called items, and then update the appointment_date field in that array element.
Here is a demo showing that the JSON replacement syntax/logic is working:
Demo
But, you could equally as well have done this JSON work in your PHP layer. It might make more sense to do this in PHP.
If you want to do this in php then, steps to follow:
Select the respective column from the table
Use json_decode to convert the string to array
Now you have the json object, apply your modifications
Use json_encode to convert your json object back to string
Save this string in table
I have a json data like this
{"0":"6","1":"5","2":"10"}
And on the DB I have table which contains json datas like these
{"0":"6","1":"4"}
{"0":"5","1":"2","2":"7"}
{"0":"3","1":"10","2":"4"}
{"0":"6","1":"5","2":"10","3":"8"}
So, I would like know is it possible or does it make sense to select data by comparing the json datas?
I would like to get any json that may contain any key:value in my input json.
So, from my example they will be these
{"0":"6","1":"4"}
{"0":"6","1":"5","2":"10","3":"8"}
You can use JSON search functions. For example -
SELECT json_field FROM table1
WHERE
JSON_CONTAINS(json_field, '{"0":"6"}')
AND JSON_CONTAINS(json_field, '{"1":"5"}')
AND JSON_CONTAINS(json_field, '{"2":"10"}');