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

OPENJSON with dynamic JSON key value pair

we are having a JSON document, where week numbers are dynamic keys. We want to load them to a relational table. We are able to achieve relational resultset, if we hardcode the weeknumbers, as given below. But, it looks like a circuitous approach…
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
0
votes
1 answer

How can I improve SQL performance on this Json Array?

In my SQL Server 2016+, I have a table with a json column, the json column has this format: { "$type": "Sample.Product, Sample", "Name": "sample", "Id": "12345", "Policies": { "$type":…
Dongdong
  • 2,208
  • 19
  • 28
0
votes
2 answers

How do i navigate the path of a json object that has namespace prefixes (eg n1:, n7: ) in sql server using OPENJSON?

I want to obtain xml files from an API (I can not get Json formats from this API). Thereafter i want to convert them to JSON and then save then to oneDrive using a logic app. (I intend to create tables from these json files in Azure SQL server) My…
0
votes
2 answers

SQL OPENJSON to Single Column

I have the data below; DECLARE @json NVARCHAR(2048) = N'{ "searchId": -1, "statuses": [ 33, 85, 90 ] }'; And perform a return from this with; SELECT searchID ,x.statuses FROM OpenJson(@json) WITH ( …
Bert682
  • 87
  • 2
  • 7
0
votes
2 answers

Traversing JSON via OpenJSON in T-SQL

Context: I have a JSON document with the following form saved to a column in a temp table in SQL Server [{"File": {"File_Name": "SCAN_6X_AHMC_AAPC_837I_11182019_11242019.txt", "Last_Modified": "Lastmodified:20191125.121049", "File_Size":…
emalcolmb
  • 1,585
  • 4
  • 18
  • 43
0
votes
1 answer

Parse JSON String with TSQL

I'm a little confused about how to parse a JSON string with TSQL. I've been watching Youtube movies for hours, but unfortunately I haven't found a solution yet. This is the…
0
votes
1 answer

Openjson is not returning the json object when defining nvarchar(max), and working by nvarchar(4000)

I try to extract a nested JSON from a JSON ([query]) if I do so, - with nvarchar(4000), it is working (unfortunatly some data are bigger) - with nvarchar(max) as json, the field remains empty... why ?? --query=null : SELECT visualContainers …
OpenStove
  • 714
  • 1
  • 11
  • 22
0
votes
2 answers

Difficulty with PATH for JSON Data in SQL Server using OPENJSON function

I am now importing JSON data into a SQL Server table where the JSON data is stored as a single column entry. I now have a more complex JSON structure and I am having difficulty with the path in the OPENJSON statement I tried creating table results…
runatyr
  • 57
  • 9
0
votes
1 answer

Pass JSON directly to SQL Server 2016

I would like to pass an JSON object to a SQL Server 2016 stored procedure without deserializing the object and let the database process the data. I'm passing in: DECLARE @json NVarChar(2048) = N'[{ "PMID": "31390573", "pubdate": "2019", …
Bill
  • 1,423
  • 2
  • 27
  • 51
0
votes
1 answer

OPENJSON syntax in a stored procedure in SQL Server 2016

I'm following the last example on this page https://www.sqlservercentral.com/forums/topic/using-msxml2-serverxmlhttp-within-stored-procedure-to-grab-source-of-html-page-and-save-to-table. It pulls the data and loads it into a table. I feel that my…
Bill
  • 1,423
  • 2
  • 27
  • 51
0
votes
1 answer

Parsing JSON from RESTful API with T-SQL

I want to consume RESTful api from SurveyHero into SQL Server, i have 2 concerns: How would i consume the RESTful API which needs BasicAuth directly in SQL SERVER. I tried to consume the API in C# and passed on the JSON data to SQL Server, and tried…
Abbas
  • 4,948
  • 31
  • 95
  • 161
0
votes
1 answer

OPENJSON not available on SQL Server 2016 with compatibility level set to 130

I am attempting to use OPENJSON in a database that is running on SQL Server 2016, and get the following error when running this simple test query (which works fine on a different 2016 database) select * from OPENJSON('{ "test": "test" }') Invalid…
mituw16
  • 5,126
  • 3
  • 23
  • 48
0
votes
1 answer

Retrieve JSON Nested Values Via OPENJSON

Need to retrieve the values of Batters/Batter/Type 1-4 Here is the JSON data [ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": …
Grim Rieber
  • 9
  • 1
  • 1
  • 7
0
votes
0 answers

Dataset has no values even though procedure is returning proper value

I have a procedure where I am using OPENJSON to display the Json string as key value pairs. When I execute the procedure, it is returning the expected value bu in controller, the dataset is empty. It only shows the table headers - key, value, type.…
Neeta
  • 11
  • 3
0
votes
0 answers

Conditionally delete a json object from Json Array in SQL Server 2017

I am using Json Array to store child table records as a column in parent table. Now I have a requirement to conditionally delete one of the record from json array. If it would have been a table, I could have used delete command with the condition…
Prakash Tripathi
  • 469
  • 6
  • 12