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
0 answers

T-SQL - Inserting 3.5M rows increases the transaction log size to max

Following is the query I am using to parse 3.5M rows of JSON data and insert it into another table. DECLARE @id_control bigint DECLARE @batchSize bigint DECLARE @results nvarchar(256) DECLARE @NextFetchValue bigint SET @results = 1 …
TechMaxy
  • 11
  • 4
0
votes
1 answer

OPENJSON is slow to parse 3M rows from a table

I have a table with 3M JSON rows. I am trying to parse the JSON and insert data into another table. But OPENJSON takes like 50 seconds to Parse the 500K rows. Query I am using select top 500000 qse.* from dbo.QueryStore qs cross…
TechMaxy
  • 11
  • 4
0
votes
2 answers

OPENJSON - How to extract value from JSON object saved as NVARCHAR in SQL Server

There is a column RawData of type NVARCHAR which contains JSON object as strings RawData ------------------------------------------------------- {"ID":1,--other key/value(s)--,"object":{--object1--}} {"ID":2,--other…
A Farmanbar
  • 4,381
  • 5
  • 24
  • 42
0
votes
1 answer

SQL Server JSON Transpose rows to columns

DECLARE @json NVARCHAR(MAX) SET @json='{"name":"John","surname":"Doe","age":45,"skills":["SQL","C#","MVC"]}'; SELECT * FROM OPENJSON(@json); This gives you key, value, and type as Columns. I want to have Name, surname and age as columns. and…
0
votes
2 answers

Parsing with OPENJSON

I am trying to take the response from a GET call and load it into SQL Server via OPENJSON, but I'm having trouble parsing the response. DECLARE @json NVARCHAR(MAX); SET @json = N'{ "LookupServiceType": "GetAssetValues", "Items": [ { …
keatklein
  • 134
  • 1
  • 1
  • 9
0
votes
1 answer

Extracting multiple values/levels from a json string in a SQL Server 2019 database table

Building on this post here How can I achieve this in SQL Server 2019? I have an order table with a lineaggregate column that contains a json string with line item details of what was shipped. I need to fetch each variant ID and quantity into…
0
votes
1 answer

Issue with dynamic SQL to get JSON properties as columns when JSON is NULL

I am trying to select JSON properties as column. The properties that needs to be selected are stored in some other table CREATE TABLE [Templates]( [ID] [int] NOT NULL, [Template] [nvarchar](max) ) INSERT INTO…
LP13
  • 30,567
  • 53
  • 217
  • 400
0
votes
0 answers

Passing JSON data from Javascript to OPENJSON MSSQL

I have set of JSON data, which can be seen in the console.log coming from Javascript, in console looks like this: 0: {datum: "18:17:00 04.08.2020", winddir: "S", windspeed: "28.7", windgust: "27.8", temp: "17.2"} 1: {datum: "18:16:00 04.08.2020",…
0
votes
2 answers

OPENJSON to ignore case when parsing JSON properties

Lets say there is a table A that has column Information, and data is stored there in JSON format. JSON string, stored there, may have properties Comment and Timestamp or properties comment and timestamp. Like this: [{"Timestamp":"2018-04-11…
Aryan Firouzian
  • 1,940
  • 5
  • 27
  • 41
0
votes
1 answer

TSQL OPENJSON when path has funky charactors

My JSON financial time series data is from alphavantage I suspect without the spaces and periods (.) in the path labels I'd be fine. I managed to get the TSQL to run without the parser fussing but not path node data. Are the special characters the…
John Ligda
  • 69
  • 1
  • 8
0
votes
1 answer

SQL Server openjson

From my json data, I need result like this: I want to get two columns distances and durations - can anyone help? { "distances": [ [0, 6136.2, 6136.2, 0], [4704.7, 0, 0, 4704.7], …
ambako
  • 11
  • 1
  • 3
0
votes
1 answer

openjson : SELECT star

I have a table in a json file…
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28
0
votes
0 answers

SQL Server : Error with OPENJSON in stored procedure

The following code works in my local instance of Sql Server but fails in remote instance with error Can you please help me out on this. Getting Error@ 102: Incorrect syntax near '$.Location' If (len(@JsonBODetails) > 0) Begin Insert Into…
0
votes
1 answer

SQL Query on Json object inside multiple JSON objects

I have the below Json string. I need to write a query to get the TP records. Without providing the index value, we need get the result. { "S": [ { "Name": "Project1", "SP": [ { "ID": 1, "Name": "Test1", …
Ashok Yaganti
  • 173
  • 5
  • 14
0
votes
1 answer

Select Json Object as nvarchar(max) in JSON_VALUE

I do not know how to extract a path of JSON as nvarchar(max). It always returns null. I do not want to specify the fields to get because I have no idea the field names. So the idea here is select it as a string and process them later. DECLARE @json…
Hieu Le
  • 1,042
  • 8
  • 18