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

Multiple cross apply in SQL for a row with json

I have a row: id, shipping_data, line_data 0, '[{"name": "Greg", "Address": "test address"}]', '[{"products": "apple", "cost": "0.50"'}' and I want to expand the row to display all data in one row. I tried using multiple cross applies: SELECT id,…
apolloSN
  • 67
  • 5
1
vote
1 answer

Add data type to JSON values query

I am building the following view on SQL Server. The data is extracted from Soap API via Data Factory and stored in SQL table. I union two pieces of the same code because I am getting outputs from API as a Objects or Arrays. This query works, however…
David
  • 27
  • 4
1
vote
1 answer

SQL Parse Json array to rows

I'm trying to parse the 'custinfo' array to rows, rather than specific columns how I have in my query (there can be none or many values in the array) DECLARE @json NVARCHAR(MAX) ='{ "customer": [ { "id": "123", "history": [ …
DC07
  • 293
  • 5
  • 18
1
vote
1 answer

Using OPENJSON in SQL Server Query

I'm trying to pull some information from a JSON array stored in an SQL Server database. I can see I need to use OPENJSON, and I've found numerous examples of how to use it, but the rub is all of the examples demonstrate declaring a single JSON array…
Steve
  • 87
  • 9
1
vote
1 answer

SQL on US Census - Null values return

I am trying to get data from the US Census on SQL Server Studio for analysis (trade data at port level). I have downloaded a JSON file for now from their API (ideally, I will do a call from SQL studio later). I then read the file with OPEN ROW SET…
Alex
  • 13
  • 2
1
vote
1 answer

How Can I use OpenJson to access double nested arrays IN T-SQL?

I have a JSON file listed below and I am having issues using OPENJSON to get access to the Misc field when the CheckboxValue is true. So the example below would return a table: Company ReferenceDuplicate Type …
apolloSN
  • 67
  • 5
1
vote
1 answer

OpenJson to parse and partially update a json column column

I have this table: CREATE TABLE [dbo].[Device] ( [Id] [bigint] IDENTITY(1,1) NOT NULL, [DeviceStatus] [int] NOT NULL, [Type] [nvarchar](64) NOT NULL, [Serial] [nvarchar](64) NOT NULL, [Group] [nvarchar](max) NULL, [Name]…
Marc
  • 2,023
  • 4
  • 16
  • 30
1
vote
1 answer

OPENJSON() optimization for poorly structured API response

I'm trying to use the TSheets API to pull data into my internal database, but the responses come back in a way that I can't figure out how to efficiently get it into table structure. I've simplified the response for demo purposes, but it basically…
Peter
  • 808
  • 2
  • 11
  • 17
1
vote
1 answer

Transform json into table rows using sql

My initial table looks like this (values of object are dynamic so it's not always the same structure): id tags 1 {"tag1": "value1"} 2 {"tagA": "valueA", "tagB": "valueB"} And I want to transform it into this: id tag value 1 tag1 value1…
Patricia
  • 95
  • 9
1
vote
2 answers

SQL Server : OPENJSON error with dynamic JSON columns to store in DB table

I am getting error with below code where I am using SQL Server JSON functions for my project. I want to try to get json key value pairs to be stored into some tables in DB. Basically I am trying to hit REST API that is returning a JSON and that I…
bj4you
  • 99
  • 1
  • 4
1
vote
1 answer

How can I find a record that equals a value inside a JSON array

I have the following Table mytable id | Json 1 | {"test":[], "partList":[{"partid": 44, "partNum": "1234"}, {"partid": 34, "partNum": "2423"}]} 2 | {"test":[], "partList":[{"partid": 23, "partNum": "8343"}, {"partid": 34,…
Ray
  • 325
  • 1
  • 5
  • 15
1
vote
1 answer

Having trouble with MS Sql Server OPENJSON feature

I created a sql test script below for this question. IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp; CREATE TABLE #temp ( Id INT NOT NULL PRIMARY KEY , Attributes NVARCHAR(MAX) NULL ); INSERT INTO #temp (Id,…
Don Bouchard
  • 69
  • 2
  • 7
1
vote
1 answer

Trying to read JSON string using SQL Query and OpenJson

I have a very large JSON string, I am trying to read using SQL for some report, but the JSON has objects embedded within in at least couple of levels, can somebody please suggest me how can I read all the Data using one SQL query using OpenJson. I…
AbdulAleem
  • 63
  • 8
1
vote
2 answers

How to parse JSON list of list using SQL Server

To parse data in a SQL Server database from an API that returns the data in the following format: declare @json nvarchar(4000) = N'{ "List":{ "header":{ "id":"id1", "resolution":"Hourly" }, "values":[ …
Javi Hernandez
  • 314
  • 8
  • 17
1
vote
1 answer

INSERT Statement Expensive Queries on Activity Monitor

I have a list of Objects that is around 300k. And I'm sending this by batch on a Web API. Since this object is a multi level I decided to send the items in batch of 1000 which converted to JSON string. At the moment, I'm seeing about 50 records…