I have a table name TestTable which contains multiple columns. Some of them are Code1,Code2,code3 and Code 4.
I want to create a JSON output which would map to a single column in backend application whose col name is App.test.code.
The JSON output should look like this:
"App": {"Test": [ {"Code":"Code1"}, {"Code":"Code2"}, {"Code":"Code3"},{"Code":"Code4"} ] }
I have tried this
Select (
Select
Code1 as App.test.code
from json path,
Include_null_values,
without_array_wrapper
)
from Testtable
But this is not giving correct output since I am only able to pick one column from SQL select statement.
Can anyone please help me on this. Thanks
Related
subcategory_id column data are in JSON format
like :
["3","4","5"]
Now I want to select all data from the table where subcategory_id = 3. How can I do this with Laravel query builder?
you can use this.
DB::table('table_name')->whereJsonContains('subcategory_id ','3')->get();
I had the same issue with whereJsonContains. Make sure you are inserting the data in a proper format, because you wouldn't get an error if you try to insert to JSON column like this:
YourTable::factory()->create([
'subcategory_id' => '3' // this should be like ['3']
]);
If the data is inserted properly, you can use whereJsonContains,
YourTable::whereJsonContains('subcategory_id', "3")->get();
Which returns all rows having "3" in subcategory_id array column.
This feature is not supported by the SQLite database
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.
I am trying to read data from json files in S3 into my Hive table. If the column names and json keys are same its all loading properly. But now I want to read data in such a way that the nested json values goes into specific columns (For eg: for json
{"data1": {"key1": "value1"}}
I want the data1.key1 value to go into column named data1_key1; which I understand is achievable with SERDEPROPERTIES. My next problem is there can be multiple json keys and I want the key names to be column values in my Hive table.
Also, depending upon those keys, the keys that go into other columns will also change.
For eg my json files will be either:
{"data1" : {"key1":"value1"}}
or
{"data2" : { "key2" : "value2"}}
This need to create a table as below:
col1 col2
data1 value1
data2 value2
Is this possible? If so how should it be done?
You can do it using regular expressions. Define json column as string in table DDL and use regexp to parse it. Tested on your data example:
Demo:
with your_table as ( --Replace this CTE with your table
select stack(2,
'{"data1": {"key1": "value1"}}',
'{"data2" : { "key2" : "value2"}}'
) as json
)
select regexp_extract(json,'^\\{ *\\"(\\w+)\\" *:', 1) as col1, --capturing group 1 in a parenthesis START{spaces"(word)"spaces:
regexp_extract(json,': *\\"(.+)\\" *\\} *\\}$', 1) as col2 --:spaces"(value characters)"spaces}spaces}END
from your_table;
Result:
col1,col2
data1,value1
data2,value2
Read the comments in the code please. You can adjust this solution to fit your JSON. This approach allows to extract keys and values from JSON not knowing their names. json_tuple and get_json_object are not applicable in this case.
Alternatively you can use regexSerDe to do the same in the table DDL like in this answer: https://stackoverflow.com/a/47944328/2700344. For the RegexSerDe solution you need to write more complex single regexp containing one capturing group (in parenthesis) for each column.
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
which form should have json code in mysql field ?
I need users datas (user_id is the key) with 3 values (3 informations : name , age, sex)
145:"name,age,sex",
148:"name,age,sex",
200:"name,age,sex"
I am using mysql version 5.6 and the datas will be inserted with SQL code
is it correct to store it in that way in mysql to retrieve with php and json_decode?
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
thank you
Do you ever want to be able to write a query like select * from users where sex=1? If so, don't store the JSON as text. Store each value (id, name, age, sex) in a column of its own.
Even if you do want to store the JSON as a string, it would probably be better organised like this:
[
{"id":236,"name":"paul","age":26,"sex":1},
{"id":2515,"name":"fred","age":42,"sex":1},
{"id":2516:"jane","age":21,"sex":0}
]
You would need to manipulate it a bit after querying, but you would have more meaningful data.
But if all you want is to store that text, so you can retrieve it as text later, then what you have is fine.
with datas in this form
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
and json_decode($myfield);
I get NULL
if I echo $myfield I get exatly what is stored in the database
what is wrong with N
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
If I parse it here http://json.parser.online.fr/
I get Syntax error