Unable to extract property with SQL JSON_QUERY [duplicate] - sql

This question already has an answer here:
Why is JSON_QUERY sending back a null value?
(1 answer)
Closed last year.
I have the folowing Json data stored in a sql table :
{"OrderNumber":"12450-OF","OrderType":"OF"}
I need to extract the OrderNumber from a sql query
The folowing statement returns null:
select
JSON_QUERY(Metadata,'$.OrderNumber') AS 'orderNumber'
from Documents
where Documents is my table, and metadata is the column where my json data is stored.

You need to use JSON_VALUE() to extract a scalar value from a JSON content. JSON_QUERY() is usually used to extract an object or an array from a JSON string.
SELECT JSON_VALUE(Metadata,'$.OrderNumber') AS 'orderNumber'
FROM (VALUES
('{"OrderNumber":"12450-OF","OrderType":"OF"}')
) Documents (Metadata)
Note, that if you want to extract more values from the stored JSON, OPENJSON() with explicit schema is another option:
SELECT *
FROM Documents d
CROSS APPLY OPENJSON(d.Metadata, '$') WITH (
OrderNumber varchar(10) '$.OrderNumber',
OrderType varchar(2) '$.OrderType'
) j

You just need to replace function Json_Query to JSON_VALUE.
JSON_VALUE use for getting value from json.
JSON_Query use for getting object from json string.
For example, if you have:
"OrderNumber":["12450-OF","12450-02"]
then your query will return object
["12450-OF","12450-02"]

Related

ORACLE JSON_VALUE to build a view. Error "single-row subquery returns more than one row"

I'm writing a view were I transform a bunch of data to a more clean state to work with it, but I'm having some trouble with a coupe of fields that basically are json stuffed in columns. They're not large ones, and I've figured out with json_value how to get some of it, but the other column has an array within the json like this:
{"field1":"string","field2":0,"field3":[{"field1":"string","field2":true}]}
The data I need is the one in field3.
I've come up with a way of getting the portion of data I need raw (the contents of field3) and the idea is to extract then the data with json_vale
--code I figured out
select rtrim(ltrim(field3_raw,'['),']')
FROM json_table (
'{"field1":"string","field2":0,"field3":[{"field1":"string","field2":true}]}'format json,
'$'columns (field3_raw varchar2 format json path '$.field3')
)
;
which outputs
{"field1":"string","field2":true}
But the bad news comes when I change the raw json to the query like this:
Extract of the table I want to query:
|METADATA|
{"field1":"string1","field2":1,"field3":[{"field1":"string11","field2":true}]}
{"field1":"string2","field2":2,"field3":[{"field1":"string22","field2":true}]}
{"field1":"string3","field2":3,"field3":[{"field1":"string33","field2":true}]}
{"field1":"string4","field2":4,"field3":[{"field1":"string44","field2":true}]}
Query used:
select metadata_raw
FROM json_table (
(SELECT metadata FROM table)format json,
'$'columns (METADATA_RAW varchar2 format json path '$.field3')
)
;
Just to get this error:
SQL Error [1427] [21000]: ORA-01427: single-row subquery returns more than one row
Is there a way to retrieve in one query all the rows transformed the way I got it with json_table to use it as a subquery to build the column with the data I need in the view?
Additional details: Oracle 19c

How to unnest string format in bigquery sql?

Hi I have a table like so, and I am trying to unnest a string in the table however been unable to do so.
id
data
1
{"$google_analytics_client_id":"xxxx","fullName":"A","phoneNumber":"+xxxxx","userId":"263175"}
2
{"$google_analytics_client_id":"xxx","fullName":"B","phoneNumber":"+xxxxx","userId":"263143"}
I am trying to get the id and userId. The data part is in string.
The current code is as below, where I plan to see what's being returned so that I can select it.
select *
from table
unnest(data)
You can use the following to retrieve a value from a JSON or JSON formatted string object:
select
id,
json_value(data, '$.userId') as userId,
from sample_data
Information on JSON functions can be found here:
https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions#json_value

use Json auto into open Json in SQL

Is this syntax possible in SQL:
SELECT *
FROM OPENJSON(SELECT * FROM FoodSara_tbl FOR JSON AUTO)
If yes, can you explain me how and why?
If no, Why? and what is the beast way instead of that?
this query work correct !!!
SELECT *
FROM OPENJSON(CONVERT(NVARCHAR(MAX),(SELECT * FROM [dbo].[temp1] FOR JSON AUTO)))
OPENJSON command give a string contain a json data as parameter
but when you generate json from table you have a pure json as result Set
and OPENJSON give a string parameter as json.
The two are different
if you run this code
SELECT * FROM [dbo].[temp1] FOR JSON AUTO
you see this result
[{"Pname":"Ava","Pregion":"German","Pcount":10},{"Pname":"Ava","Pregion":"UK","Pcount":5}]
if put this result on OPENJSON
SELECT *
FROM OPENJSON([{"Pname":"Ava","Pregion":"German","Pcount":10},{"Pname":"Ava","Pregion":"UK","Pcount":5}])
see below error
Invalid column name '{"Pname":"Ava","Pregion":"German","Pcount":10},{"Pname":"Ava","Pregion":"UK","Pcount":5}'.
but if you add ' at the first and foremost of your json. its parse correct
' is sign of string in SQL Server

Searching an element in top-level json array oracle

I have an array of strings stored in oracle column as a json array in the following format:
["abc", "xyz"]
["cde", "fgh"]
["xyz"]
I have to write a query to check whether a given string is present in any of the arrays in any of the rows. In the above example I would like to see whether "xyz" is present. How should the json path be? I know I can use the 'like' clause but I don't think that is a neat way to do.
Why the query SELECT JSON_QUERY(my_column, '$[*]') FROM my_table is always returning null?
I did the following test, this may be what you are looking for:
create table t(json_v varchar2(40))
insert into t values('["abc", "xyz"]');
insert into t values('["cde", "fgh"]');
insert into t values('["xyz"]');
SELECT *
from t, json_table(t.json_v, '$[*]' columns (value PATH '$'))
WHERE value = 'xyz'
Output Result
JSON_V value
["abc", "xyz"] xyz
["xyz"] xyz
Your question two why the query always returns zero as you have to wrap the values see the JSON_QUERY syntax
SELECT JSON_QUERY(json_v, '$[*]' WITH WRAPPER) AS value FROM myTable;

MSSQL Search inside a JSON

How can i filter inside a json file in SQL server?
i have a Column call details.
{"test","source":"web"}
i want to filter by source
what i did:
select * from TABLE_NAME
CROSS APPLY OPENJSON(details,'$.source')
where value ='web'
As per Zohar's comment, make your json valid, then something like:
--{"mode":"test","source":"web"}
select * from TABLE_NAME
CROSS APPLY
OPENJSON(details)
WITH (
m varchar(256) '$.mode',
s varchar(256) '$.source'
) j
where
j.w = 'web'
But it might suit you better/simpler to just use JSON_VALUE:
select * from TABLE_NAME
WHERE json_value(details, '$.source') = 'web'
Use CROSS APPLY OPENJSON if you want to turn each row's json into a pseudotable looking like the table spec in the WITH clause. SQLServer behaves as if all the matching "rows" in each row's json are compounded into the psseudotable and auto-joined to the source data table based on where each bunch of json pseudorows came from
Use JSON_VALUE if you only really want one value out of the json and can uniquely identify a single "row" in the json from which to get the value.. Either the json only has one "row" / is not a collection, or you want a "row" out of a json collection that can be referenced according to a formula