MySql, How to select data by comparing 2 Jsons - mysql

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"}');

Related

MYSQL Json query

I have a JSON array of objects in a MySQL table that I am trying to see if there is a way to query and just pull the data. For example.
JSON Array Object
email_address_dump
[{"value":"a123#yahoo.com","type":"personal"},{"value":"all123#hotmail.com","type":"personal"},{"value":"car_sq5#indeedemail.com","type":"personal"}]
is there a way to query out just the email address? so that the results can be something like this?
a123#yahoo.com, all123#hotmail.com, car_sq5#indeedemail.com
I am not trying to search within the column, I know that with JSON Obtains you can use a where clause, this is more of a JSON Extract.
I was able to solve this by using JSON Extract from MySQL.
json_extract(c.email_address_dump, ''$[*].value') as EmailAddressArray,

How to query an array field (AWS Glue)?

I have a table in AWS Glue, and the crawler has defined one field as array.
The content is in S3 files that have a json format.
The table is TableA, and the field is members.
There are a lot of other fields such as strings, booleans, doubles, and even structs.
I am able to query them all using a simpel query such as:
SELECT
content.my_boolean,
content.my_string,
content.my_struct.value
FROM schema.tableA;
The issue is when I add content.members into the query.
The error I get is: [Amazon](500310) Invalid operation: schema "content" does not exist.
Content exists because i am able to select other fiels from the main key in the json (content).
Probably is something related with how to perform the query agains array field in Spectrum.
Any idea?
You have to rename the table to extract the fields from the external schema:
SELECT
a.content.my_boolean,
a.content.my_string,
a.content.my_struct.value
FROM schema.tableA a;
I had the same issue on my data, I really don't know why it needs this cast but it works. If you need to access elements of an array you have to explod it like:
SELECT member.<your-field>,
FROM schema.tableA a, a.content.members as member;
Reference
You need to create a Glue Classifier.
Select JSON as Classifier type
and for the JSON Path input the following:
$[*]
then run your crawler. It will infer your schema and populate your table with the correct fields instead of just one big array. Not sure if this was what you were looking for but figured I'd drop this here just in case others had the same problem I had.

Postgresql merge/aggregate json column and return json object as output

I am using Postgresql 9.6
I have a table, where my column is of type json.
create table test (
my_data json,
.
.
);
When queried, for each row the column is shown as json:
I want to aggregate the data, for sake of simplicity selected 2 columns only. I need to group by col1_data. I want like below:
I tried to use json_agg, but that will agregate and output as array of jsons.
select col1_data, json_agg(col2_data) as col2_data
from test
group by col1_data;
Can someone help to convert the array of json to json?
This should do it:
select col1_data, json_agg(col2_element) as col2_data
from test, json_array_elements(col2_data) as col2_element
group by col1_data;
Alternatively, you can write your own aggregate function that concatenates json arrays.

JSON update single value in MySQL table

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

Incorporating JSON objects in a table into JSON output

I have two tables with some data:
Table 1: A single row of data actin as header information to the data contained in table 2.
Table 2: A table with multiple rows including various data columns, but also including two columns with JSON formatted text (varchar(max)).
The sample data is available here:
https://docs.google.com/spreadsheets/d/1Y1Zb2a2G-NZ71wNLQxTTeiF8gXdLuIRfUrqBVmAdTFU/edit#gid=0
The requirement is to wrap the data in Table 2 within Table 1 producing a single valid JSON output including the two columns with JSON formatted text.
I expect the output to be produced by something looking like this:
SELECT *
,(
SELECT *
FROM Table2
FOR JSON path
) elements
FROM Table1
FOR JSON path
I am not sure what the best way is to incorporate the JSON formatted text into this output. I suspect that one will have to use JSON_modify() to achieve this.
Do you have any suggestions?
The answer was actually quite simple. The statement merely requires JSON_Query() statements for the two columns with formatted JSON text to get the required output.
SELECT *,
(
SELECT
SequenceID,ItemID,Description,arrow,arrowColor,Value,valueUnit,volatility,BulletChartID,JSON_Query(BulletChart) BulletChart,JSON_Query(TrendLineChart) TrendLineChart
FROM Table2
FOR JSON path
) elements
FROM Table1
FOR JSON path