json sql with list items - sql-server

I have json stored in sql2016. when I run a query parts of the json returns value and other parts don't return value.
here is a json stored in table dbo.Trades
The datatype is as follows
CREATE TABLE [dbo].[Trades](
[_id] [int] IDENTITY(1,1) NOT NULL,
[trade] [nvarchar](max) NULL,
My json is formatted as below and stored in trade column
{
"direction": "Short",
"Powerbars": 2,
"PowerbarsTime": [
"2016-11-10T20:25:32.481424-05:00",
"2016-11-10T20:44:01.8993031-05:00"
]
}
Since Powerbarstime is a list in the json, I have to provide the index to print it. however I don't know how many entries will eb there in the list. Is there a way to merge the list which is part of the json and return a comma delimited column?
I have to do this to print
SELECT _id,
JSON_VALUE(trade, '$.Powerbars') AS powerbars,
JSON_VALUE(trade, '$.PowerbarsTime[0]') AS PowerbarsTime1,
JSON_VALUE(trade, '$.PowerbarsTime[1]') AS PowerbarsTime2
FROM dbo.Trades
order by _id desc
I get the 2 items on the list that I hardcode into the sql.
How can I merge all the list items under JSON_VALUE(trade, '$.PowerbarsTime') so I can see it all as 1 comma delimited column.

Related

Select the values from a JSON aray list from a column in a table

I need to run a query on a column of a table row to return the data as a list, the data in question is formatted as a JSON array, how do I select all the 'names' as name and values as a value to display as a list?
The table is produced from a Joomla 4 CMS using the custom fields if that is of any help.
the data in the column is formatted this way
{"multiple":0,"options":{"options0":{"name":"Select Make","value":"Select"},"options1":{"name":"Abarth","value":"Abarth"},"options2":{"name":"Alfa Romeo","value":"Alfa Romeo"},"options3":{"name":"Audi","value":"Audi"},"options4":{"name":"BMW","value":"BMW"},"options5":{"name":"Chevrolet","value":"Chevrolet"},"options6":{"name":"Chrysler","value":"Chrysler"},"options7":{"name":"Citroen","value":"Citroen"},"options8":{"name":"Cupra","value":"Cupra"},"options9":{"name":"DS","value":"DS Automobiles"},"options10":{"name":"Dacia","value":"Dacia"},"options11":{"name":"Daewoo","value":"Daewoo"},"options12":{"name":"Daihatsu","value":"Daihatsu"}}}
I have tried this query, but it returns nothing
SELECT JSON_OBJECT ('name', 'name', 'value', 'value') `fieldparams`FROM `josl2_fields` WHERE `title`='manufacturer'
Column in a table row
MYSQL Table
Any help would be appreciated
For this (in joomla to), I transform all the JSON in array with json_decode first, and I browse the array

BigQuery ARRAY_TO_STRING based on condition in non-array field

I have a table that I query like this...
select *
from table
where productId = 'abc123'
Which returns 2 rows (even though the productId is unique) because one of the columns (orderName) is an Array...
**productId, productName, created, featureCount, orderName**
abc123, someProductName, 2020-01-01, 12, someOrderName
, , , , someOtherOrderName
I'm not sure whether the missing values in the 2nd row are empty strings or nulls because of the way the orderName array expands my search results but I want to now run a query like this...
select productName, ARRAY_TO_STRING(orderName,'-')
from table
where productId = 'abc123'
and ifnull(featureCount,0) > 0
But this query returns...
someProductName, someOrderName-someOtherOrderName
i.e. both array values came back even though I specified a condition of featureCount>0.
I'm sure I'm missing something very basic about how Arrays function in BigQuery but from Google's ARRAY_TO_STRING documentation I don't see any way to add a condition to the extracting of ARRAY values. Appreciate any thoughts on the best way to go about this.
For what I understand, this is because you are just querying one row of data which have a column as ARRAY<STRING>. As you are using ARRAY_TO_STRINGS it will only accept ARRAY<STRING> values you will see all array values fit into just one cell.
So, when you run your script, your output will fit your criteria and return the columns with arrays with additional rows for visibility.
The visualization on the UI should look like your mention in your question:
Row
productId
productName
created
featureCount
orderName
1
abc123
someProductName
2020-01-01
12
someOrderName
someOtherOrderName
Note: On bigquery this additional row is gray out ( ) and Its part of row 1 but it shows as an additional row for visibility. So this output only have 1 row in the table.
And the visualization on a JSON will be:
[
{
"productId": "abc123",
"productName": "someProductName",
"created": "2020-01-01",
"featureCount": "12",
"orderName": [
"someOrderName",
"someOtherOrderName"
]
}
]
I don't think there is specific documentation info about how you visualize arrays on UI but I can share the docs that talks about how to flattening your rows outputs into a single row line, check:
Working with Arrays
Flattening Arrays
I use the following to replicate your issue:
CREATE OR REPLACE TABLE `project-id.dataset.working_table` (
productId STRING,
productName STRING,
created STRING,
featureCount STRING,
orderName ARRAY<STRING>
);
insert into `project-id.dataset.working_table` (productId,productName,created,featureCount,orderName)
values ('abc123','someProductName','2020-01-01','12',['someOrderName','someOtherOrderName']);
insert into `project-id.dataset.working_table` (productId,productName,created,featureCount,orderName)
values ('abc123X','someProductNameX','2020-01-02','15',['someOrderName','someOtherOrderName','someData']);
output
Row
productId
productName
created
featureCount
orderName
1
abc123
someProductName
2020-01-01
12
someOrderName
someOtherOrderName
2
abc123X
someProductNameX
2020-01-02
15
someOrderName
someOtherOrderName
someData
Note: Table contains 2 rows.

Unable to extract value from json array column in BigQUERY

I have a table column in BigQuery with JSON array format with a row like {"role":"SuperAdmin","_id":"abcd","userId":"efgh"}. This column schema in BigQuery is REPEATED mode. My goal is to extract the userId value for all the rows in that column.
I have tried using JSON functions like json_value and json_extract:
select json_value(column_name, '$.users.userId') as userId, from table_name
but get the following error :
No matching signature for function JSON_VALUE for argument types: ARRAY<STRING>, STRING. Supported signature: JSON_VALUE(STRING, [STRING]) at [2:3]
Please how do I go about it?
Because it is repeated you will need to unnest it first.
Given the following example:
with sample_data as (
select ['{"role":"SuperAdmin","_id":"abcd","userId":"efgh"}','{"role":"SuperAdmin","_id":"abcd","userId":"efgh"}','{"role":"SuperAdmin","_id":"abcd","userId":"efgh"}'] as users
)
select json_value(user, '$.userId') as userId
from sample_data , UNNEST(users) user
The return is:

convert one column to json

Using Azure SQLServer I'm trying to convert only one column to json format. The data is in a nvarchar field. Using For Json Auto will convert all fields and all rows. What I need is just to convert only one column.
By converting to json what I mean is to be clickable to see it's data a new window inside SSMS.
Let's say the table (Logs) has 2 columns: LogId and LogData. LogData is nvarchar(max) and contains json data.
What I need is to query the table and have a clickable logData column.
You can try like following to get only one column as JSON
select o.*,
(select YourColumnForJson
from YourTable i
where o.Identifer=i.Identifer for json auto) as JsonColumn
from YourTable o

Array structures querying in presto, hive

col-1 has dep_id(varchar) -
112
col-2 has array struct
[
{
"emp_id": 8291828,
"name": "bruce",
},
{
"emp_id": 8291823,
"name": "Rolli",
}
]
I have a use case where i need to flatten and display results. For example when queried data for dep_id - 112 I need to display emp_id in a separate row.
For above data when queried my result should look like
id emp_id
112 8291828
112 8291823
What should be my query format to fetch data?
There are several parts to make this work. First the JSON data will appear as a VARCHAR, so you first need to run json_parse on it to convert it to a JSON type in the engine. Then you can cast JSON types to normal SQL structural types, and in your case this is an array of rows (see cast from JSON). Finally, you do a cross join to the array of rows (which is effectively a nested table). This query fill give you the results you want
WITH your_table AS (
SELECT
112 AS dep_id
, '[{"emp_id": 8291828, "name": "bruce"}, {"emp_id": 8291823, "name": "Rolli"}]' AS data
)
SELECT
dep_id
, r.emp_id
, r.name
FROM your_table
CROSS JOIN
UNNEST(cast(json_parse(data) as array(row (emp_id bigint, name varchar)))) nested_data(r)

Resources