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
64
votes
3 answers

SQL Server OPENJSON read nested json

I have some json that I would like to parse in SQL Server 2016. There is a hierarchy structure of Projects->Structures->Properties. I would like to write a query that parses the whole hierarchy but I don't want to specify any elements by index…
Slade
  • 2,311
  • 3
  • 21
  • 25
51
votes
4 answers

SQL Server Invalid version: 15 (Microsoft.SqlServer.Smo)

Context: I'm having difficulty modifying a stored procedure in SQL Server 2016. The stored procedure performs parsing of json data within a file. For some reason I'm able to execute the stored procedure and it executes successfully but when I try to…
emalcolmb
  • 1,585
  • 4
  • 18
  • 43
7
votes
1 answer

How to use OPENJSON on multiple rows

I have a temp table with multiple rows in it and each row has a column called Categories; which contains a very simple json array of ids for categories in a different table. A few example rows of the temp table: Id …
Quiver
  • 1,351
  • 6
  • 33
  • 56
6
votes
1 answer

I am trying to connect to abfss directly(without mounting to DBFS) and trying to open json file using open() in databricks

I am trying to connect to abfss directly(without mounting to DBFS) and trying to open json file using open() method in databricks. json_file = open("abfss://@.dfs.core.windows.net/test.json') databricks is unable to open file present in azure blob…
5
votes
1 answer

OPENJSON collation in Azure Synapse causes a collation conflict error

I have an OPENJSON command that takes the parsed JSON and LEFT joins it onto an existing table. When I add the LEFT JOIN I get the error: collation conflict between "Latin1_General_BIN2" and "SQL_Latin1_General_CP1_CI_AS" The table has the same…
Slicc
  • 3,217
  • 7
  • 35
  • 70
5
votes
3 answers

How to Set OPENJSON Path to Nested Array

I'm trying to set the path for my OPENJSON function for the nested array, but it's not working. Tried different variations and examples/resources I found online and still cannot figure it out. Any ideas? EDIT: To be clear, I know how to do this with…
ptownbro
  • 1,240
  • 3
  • 26
  • 44
5
votes
1 answer

Select results from multiple array elements in Json document with Sql Server 2016's OPENJSON function

Is it possible to combine parts of a json document from multiple array elements into a single result in Sql Server 2016? Given this json: { "fruit": { "types": [ { "possible": [ "Apples", "Bananas", "Pears" ], "category":…
Dave Slinn
  • 435
  • 6
  • 13
4
votes
2 answers

How to get the detail row number for rows in JSON string

I have a stored procedure that accepts a NVARCHAR(max) string that is JSON data that I need to validate before loading it into the live tables. If the validation fails I need to return a message with the issue and the row number of the bad data row.…
Keith Miller
  • 115
  • 5
  • 11
3
votes
1 answer

How do I get the "key" of an array item in OPENJSON, when using WITH?

I want to parse a json-array in my sql-server. I would like to use OPENJSON with WITH to parse specific values into columns. How can I get the index of each array item? I know, that this works fine with JSON_VALUE and without WITH: DECLARE @json…
slartidan
  • 20,403
  • 15
  • 83
  • 131
3
votes
2 answers

Accessing OPENJSON parsed JSON multiple times in a stored procedure?

Consider this JSON: { "Name": "Alice", "Relations": [ { "RelationId": 1, "Value": "one" }, { "RelationId": 2, "Value": "two" } ] } I pass this JSON to a…
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
2
votes
2 answers

JSON Object Query SQL Server

I have a JSON string which is the following: [ { "id": 103001058774, "name": "status", "label": "Status", "description": "Ticket status", "choices": { "2": [ "Open", …
AshJam
  • 35
  • 4
2
votes
1 answer

SQL Server parse nested json using OPENJSON

I am trying to read JSON that I would like to parse each collection in SQL Server. The structure is as follows Root > Action > GoActionFiles / Pools I would like to return a row for each GoActionFiles > GoActionFile, so it looks like the below; The…
nowYouSeeMe
  • 935
  • 6
  • 11
  • 21
2
votes
2 answers

SQL Server 2019 - Update Table by JSON Array of object

I have a table like this: CREATE TABLE WeeklySlots ([dow] int, [slots] int, [SlotCode] varchar(6)) ; INSERT INTO WeeklySlots ([dow], [slots], [slotCode]) VALUES (1, 0, 'T19_00'), (2, 20, 'T19_00'), (3, 20, 'T19_00'), (4,…
Joe
  • 1,033
  • 1
  • 16
  • 39
2
votes
1 answer

sql-server OPENJSON ways to parse multiple array value elements as one element?

Is there is any other (more pretty) way to accomplish the following? I am struggling with a way to display multiple elements of an array as one element. I have a JSON-string, that looks like this: DECLARE @JSON nvarchar(max) SELECT @JSON =…
KirstenO
  • 23
  • 2
2
votes
1 answer

Using SQL Server Indexed View in combination with OPENJSON

I have a table with just one row and one column which stores a JSON array with about 30MB/16k objects in it: CREATE TABLE [dbo].[CitiesTable] ( [CitiesJson] [NVARCHAR](MAX) NOT NULL ) ON [PRIMARY] GO INSERT INTO [dbo].[CitiesTable]…
Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
1
2 3
13 14