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

Optimal way to filter table rows based on a variable JSON array in SQL using JOIN or other methods?

I have a table A with a large number of rows and an id column. Additionally, I have a variable @Json that can have three types of values: null, an empty array '[]', or a JSON array like '[1,2,3]'. I am looking for the optimal way to filter the rows…
Alexander
  • 43
  • 1
  • 6
0
votes
0 answers

OPENJSON SQL Server Error: Invalid column name 'property-with-dash'

I am working in SQL Server and I Need to select "name-surname" tag from below mentioned JSON but getting following error: JSON path is not properly formatted. Unexpected character '-' is found at position 11. do we have any good work around to read…
VAIB TAG
  • 5
  • 2
0
votes
2 answers

Convert JSON value in single row in TSQL 2019

I need help in transforming a string to json to put the values in separated lines. Using tsql (2019) How do I have to transform the provided data (payload) into a json to create the expected result or is there another way to splitt the data? Thank…
0
votes
2 answers

Get different values from JSON array

I have a SQL table containing 2 columns - first Column is the ID of the users and the second column holds the JSON Phone information. The phone information consist of Work, Home phones and whether it is a mobile or a landline. It is not always that…
OJ Slott
  • 75
  • 6
0
votes
1 answer

Getting NULL with OPENJSON read JSON with SQL

I have JSON like below. and I want to find all models from the JSON. When I give below SQL query it gives me only Sedan models DECLARE @PermsJSON NVARCHAR(MAX) =; select * from OPENJSON(@PermsJSON, '$.Models[0].Make') But, when I gave below…
0
votes
0 answers

SQL Server settings for OPENJSON

I have an instance of SQL Server running Microsoft SQL Server 2017 (RTM-CU31) (KB5016884) - 14.0.3456.2 (X64) In one database, if I use JSON functions I get the following error: Invalid object name 'OPENJSON' But in another database on the same…
user1707389
  • 99
  • 1
  • 4
  • 13
0
votes
0 answers

Default value for column is not set at moment of insert using openjson

Consider the following statement to create a table, note the default value for MyDateTime: USE [MY_DATABASE] GO ALTER TABLE [dbo].[MyTable] DROP CONSTRAINT [DF_MyTable_MyDateTime] GO /****** Object: Table…
0
votes
0 answers

SQL OpenJson - parse json to table

Trying to get JSON string as table in SQL . JSON is sent dynamically and based on that need to form the SQL Table querying the JSON dynamically as the JSON structure may vary for each request Am trying to get key element as column name and value…
Peru
  • 2,871
  • 5
  • 37
  • 66
0
votes
1 answer

OPENJSON - parsing nested JSON preserving relations

I have a JSON array that looks like this [ { "_id": "12345", "uniqueId": null, "companyName": "ABC Corp", "yearFounded": 1958, "stateFounded": "Delaware", "someField": [ { "primaryCode": "14", …
Dmitriy Ryabin
  • 363
  • 3
  • 16
0
votes
1 answer

Json input to Sql table

I've a requirement to load the json input into a table which has multiple arrays declare @json nvarchar(max); set @json = '{ "method":"email", "value":"xxx@gmail.com", "options":[ { …
vinay kumar
  • 41
  • 1
  • 6
0
votes
1 answer

SQL Server update JSON with url in a property without introducing escaping sequences

I am trying to update an array in a JSON contained into a column inside my DB, it's not clear to me what i did wrong but seems like that when a JSON property (a string) contain backslash, those value will be escaped. I don't want that escaping is…
Skary
  • 1,322
  • 1
  • 13
  • 40
0
votes
2 answers

Parse Google API JSON file to rows and columns with OPENJSON in T-SQL

So I am trying to create a query than can handle a json file that we get with a data factory web request from the Google Analytics API 4 and store the result in an Azure sql table. The following query is the closest I got. The dimension and metric…
0
votes
1 answer

OPENJSON select value by dynamic key

data row 1 : { "30":{"status":0,"approval":"0","entrydate":"2023-01-30"}, "26":{"status":0,"approval":"0","entrydate":"2023-01-30"} } data row 2 : { "12":{"status":0,"approval":"0","entrydate":"2023-01-30"}, …
0
votes
1 answer

SQL: The multi-part identifier could not be bound with OPENJSON

I'm trying get objects from JSON by this query SELECT co.contract_number , co.objectId id1 , cbs.id id2 , co.summary FROM ( SELECT c.contract_number , cb.summary , cbo.id objectId FROM …
0
votes
0 answers

SQL Server Expand JSON to Columns - THIS IS NOT about Pivoting Table

I have a use case where I want to convert json stored in a table to be expanded as columns. Now the issue is that Json contains object + arrays + nested array e.g. {prop1: "Hi", prop2Arr: [{aP1: "Bye", aP2: "Bye2", aP3: "Bye3" }, {aP1: "Bye", aP2:…
Adeel Rizvi
  • 114
  • 2
  • 4