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
Related
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
I have a Postgres query like this
SELECT * FROM my_table WHERE status IN (2,1);
This is part of a big query, but I am facing an issue with the WHERE IN part here. I am using this query inside a function and the input parameters are in JSON format. Now the status values I am getting in in the form of a JSON array and it will be like status=[2,1]. I need to use this array in the WHERE clause in the query and not sure how to do that. Currently, I am using like
SELECT * FROM my_table WHERE status IN (array([2,1]));
But this is giving me an error. The status column is of smallint data type. I know this is simple, but I am very much new to Postgres and could not figure out any method to use the JSON array in WHERE IN clause. Any help will be appreciated.
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"]
I have a table that has a JSON list as one of its values. The column name is list_order and the value would be something like: [1,2,3].
I am having trouble doing a WHERE comparison to select by list_order. In pure SQL, it would be: SELECT * FROM table_name list_order=[1,2,3];
The closest example I found was this: How do I query using fields inside the new PostgreSQL JSON datatype?. However, this grabs the value of a key in the JSON where the JSON is a dictionary and not a list. I've tried modifying it to suit my need but it did not work.
Any suggestions? Is that even possible? Why is not documented? Thanks!
I found the answer. I need to compare it as text:
"SELECT * FROM table WHERE list_order::text='[1,2,3]';
I have a something like this in my table column:
{"InputDirection":0,"Mask":"AA","FormatString":null,"AutoCompleteValue":null,
"Filtered":"0123456789","AutoComplete":false,"ReadOnly":true}
What I want to do is to change A to N in "Mask":"AA" and remove "Filtered":"0123456789" if they exist. Mask could be in different forms like A9A, 'AAAA`, etc.
If it was in C# I could do it by myself by parsing it to JSON, etc but I need to do it within SQL.
I've found this article which shows how to parse JSON to Table. This gave me an idea that I can parse each field to temp table and make the changes on that and convert it back to JSON so update the actual field where I take this JSON field from. However, this looks like a cumbersome process for both me and the server.
Any better ideas?
You can use this LINK .
And then use the following code
select * into #demo from
(Select * from parseJSON('{"InputDirection":0,"Mask":"AA","FormatString":null,"AutoCompleteValue":null,
"Filtered":"0123456789","AutoComplete":false,"ReadOnly":true}
')) a
select * from #demo
--- CHANGE THE DATA HERE AS REQUIRED
DECLARE #MyHierarchy JSONHierarchy;
INSERT INTO #myHierarchy
select * from #demo;
-- USE THIS VALUE AND UPDATE YOUR JSON COLUMN
SELECT dbo.ToJSON(#MyHierarchy)
drop table #demo
I may be getting something wrong here but why can’t you simply use REPLACE to update what’s needed and LIKE to identify JSON strings that should be updated?
update table_T
set json_string = REPLACE(json_string, '"Filtered":"0123456789",', '')
where json_string like '%"Mask":"AA"%'
Not sure I understand why do you need to parse it….