Questions tagged [open-json]

OpenJson is a T-SQL (SQL Server) built in table valued function, introduced in 2016 version. Use this tag with questions related to this command.

OpenJson was introduced in SQL Server 2016.
It takes in a nvarchar containing JSON data, with an optional nvarchar string containing a JSON Path Expression, and an optional With clause.

When the With clause is not supplied, OpenJson will return a table containing three columns:
Key (nvarchar(4000)), Value(nvarchar(max)) and Type (int).
values of the Type columns indicates the following:

Type    JSON data type
0       null
1       string
2       int
3       true/false
4       array
5       object

The With clause allows the user to explicitly define the columns of the table returned.
The With clause contains three parameters for each value to get from the JSON:
Column name, data type, and an optional JSON Path expression (if not supplied, OPENJSON will try to get a property with the same name as the target column.

If the json property extracted contains an inner json object, the AS JSON option can be specified with this column, to enable further JSON processing.

Articles

197 questions
0
votes
1 answer

How to insert date from JSON to SQL database

I have to insert some data from JSON to SQL database so I use below code: sqlQuery1 = "DECLARE @json varchar(max)='" + inputData + "';"; sqlQuery2 = "INSERT INTO [Test].[dbo].[Work] " + "SELECT [Id], [Created], [Assignee] "…
Pallavi
  • 29
  • 3
0
votes
0 answers

SQL Server OPENJSON Nested Object Syntax

I'm trying to figure out the appropriate syntax to query the Note column from only the "1" object under "worklistData"...and failing miserably. I've gone through several iterations, but the code below seems to be what "should" be working (but…
0
votes
2 answers

Is there a way to reserve generated ID on a temporary table

I posted this question INSERT Statement Expensive Queries on Activity Monitor As you will see the XML structure has different levels. I have created different tables Organisation = organisation_id (PRIMARY_KEY) Contacts = organisation_id…
0
votes
1 answer

Extract values from an SQL column with JSON format

I need to extract values from a column which has a JSON format. Here is an example of one of rows in table for this column, here is two operatorKeys but in other rows there might be more: { "officeId": "office8685", "TypeOfOffice":…
Florentina
  • 85
  • 1
  • 1
  • 10
0
votes
1 answer

Union Results of Multiple OPENJSON calls

I have a table that stores 1 json object per row, I want to call OPENJSON on the json object stored in each row and union together all of the results. I don't know the number of rows I will have ahead of time. Here is some example data to reference …
0
votes
1 answer

SQL OpenJson - Cannot create a column for json inside of json

I've been playing around with OPENJSON in sql and have a question. Say I have the following JSON, [ { "id" : 2,"firstName": "John", "lastName": "Smith", "age": 25, "dateOfBirth": "2007-03-25T12:00:00", "data":{"$source":"Online"} }, ] I am able…
Greg
  • 476
  • 9
  • 23
0
votes
1 answer

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?
0
votes
2 answers

How to work out the path for all values in a JSON

I have a JSON column that has about 300k worth of properties, and I want to attempt to export it in such a way that I can get the full path and value of the property. Note, my JSON has no arrays - it's basically a JSON for a web application…
John McDonnell
  • 753
  • 1
  • 8
  • 24
0
votes
1 answer

Dealing with nested JSON in SQL OPENJSON WITH command?

I have JSON value that has some structure to it but I'm struggling to get to the 3rd level. I am using CROSS APPLY OPENJSON to get to the "Lines" data but I need to get the "Code" out of the TaxCode area... It seems to be it's own JSON array…
Tommy Sharp
  • 65
  • 2
  • 8
0
votes
1 answer

Insert data into a table using OPENJson

I have the following Json that needs to be inserted into a table {"errorMessage":"","requestOutput":{ "REQUEST_ID" : 100910, "STATUS_CODE" : "P", "PHASE_CODE" : "N", "COMPLETION_TEXT" : null }} This is the query i am using to process the…
jaiya
  • 53
  • 4
0
votes
1 answer

Remove elements from a JSON array, with no key

I'm looking for a way that I can remove elements from a JSON array in SQL Server, I tried using JSON_MODIFY/OPENPATH, but I can't seem to get the path parameter correct. Here is an example of what I want to do is…
John McDonnell
  • 753
  • 1
  • 8
  • 24
0
votes
1 answer

T-SQL JSON: JSON_QUERY is returning a json string after comparing the object with a string

I'm trying to return a JSON_QUERY field only if the JSON contains a specific field/property, but when I compare it with a string (that is the field's name), the function doesn't returns a JSON... Single code without comparison: SELECT Field1,…
0
votes
1 answer

How to parse a json string to flat rows in SQL Server?

I have data like below in a file which I read into a variable in SQL Server: { "TypeCode": [ { "DestinationValue": "Private", "SourceValue": "1" }, { "DestinationValue": "Public", "SourceValue": "2" …
user12823830
0
votes
1 answer

how to add key-value pair in json root node and convert it into table using SQL server

I have table people and it's maintain Four column which is Name ,TagName ,Value , Location. I want to convert the tagname and value in json with name and location column as rootnode (Name & location same for multiple records) Need output as : { …
0
votes
0 answers

SQL - Insert json array into multiple tables with identity

Here is the example json - "Root Node - Type 1": { "Attributes": { "Items": [ { "A": { "Value": "A1" …
Mm77
  • 85
  • 1
  • 12