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
2
votes
1 answer

SQL Server Openjson to fetch details into a table

I have the following JSON and expecting a table out of it. { "Response": [ { "questionId": 1, "answer": [ "5","6" ] }, { "questionId": 2, "answer": [ "5" ] } ] } And the…
Raj
  • 31
  • 3
1
vote
1 answer

Using param in SQL stored procedure select value

I have a stored procedure that I am using to extract values from a JSON column in a table: @ReferenceID int, @BureausID int, @EmployersID int, @FileType varchar(12), @TargetIndex int Code: SELECT SalesOrderJsonData.* FROM…
AlliDeacon
  • 1,365
  • 3
  • 21
  • 35
1
vote
1 answer

Extract head of an array using open_json?

I am have sample JSON as below { "12345": { "percent": 26, "avgPrice": 32, "avgNewOfferCount": 12, "avgUsedOfferCount": -1, "isFBA": true, "lastSeen": 653 } } I am getting the array values but…
1
vote
2 answers

SELECT * FROM OPENJSON -- Not getting expected values

I am getting 0 row when selecting from OPENJSON below. I am expecting to get all 3 values "AA" for different as_of_date for CREDIT_RTG . I tried different select statements. I am running in SQL 2016. Expected result should be similar to this enter…
Overflow
  • 11
  • 2
1
vote
1 answer

Can I use dot navigation when using multiple parameters to filter (compare) in SQL SELECT?

I new in SQL, I want to retrieve only the records of a certain groupID and I am using this query: SELECT @Date, Capacity, UserOrUserGroup FROM UCTimePhaseMonthly WHERE Date >= 2023-05-01 AND UserOrUserGroup.id =…
mrbangybang
  • 683
  • 1
  • 9
  • 22
1
vote
1 answer

Explode/normalize a column with JSON stored as a string into rows and columns

I am using SQL Server and have a column that has JSON stored as a string in it. I am trying to explode/normalize the JSON into new rows and columns. Below is a picture of how the table currently looks. Each PricePointId has at least one record in…
1
vote
1 answer

Using OPENJSON in SQL Server to parse a Non-Array Object

I'm using SQL Server v15, called from a .NET application. A website I'm using (not mine - I don't control the data) has a JSON dataset formatted strangely. Instead of being an array like: [{"id":"1","Name":"Charlie"},{"id":"2","Name"="Sally"}] It's…
Scott
  • 3,663
  • 8
  • 33
  • 56
1
vote
1 answer

Parse JSON Column containing multiple arrays

I'm trying to get this result but I'm not getting it. I've searched the forum but I can't find a solution to my problem. Can you help me and explain what is wrong with my query? thanks. JSON { "items": [ { "id": 40054, …
1
vote
0 answers

Deserialise multiple objects into a select statment

In a table, I store multiple string records in several records. declare @x nvarchar(max) = { "totalSize": 1000, "done": true, "records": [ { "attributes": { "type": "Contract", "url": "" }, "Name":…
apolloSN
  • 67
  • 5
1
vote
2 answers

Use openjson to get columns from JSON with multiple arrays of elements

JSON input looks like this: { "reporting.unit": [ "F-1", "F-2", "F-3"], "notional.lc": [ 100.1, 140.2, 150.3] } Desired Output: reporting.unit notional.lc F-1 100.1 F-2 140.2 F-3 150.3 Note I have upwards of 20 columns and many…
Den. A.
  • 13
  • 3
1
vote
0 answers

SQL Server OPENJSON read nested JSON same key array

I am following the idea from Ed.Schavelev on article 'SQL Server OPENJSON read nested json'. But I need instead of repeating the rows I wanted them (4) to be in the columns. Is this possible? I have tried as below: DECLARE @json NVARCHAR(MAX) SET…
Sanjoy
  • 51
  • 6
1
vote
1 answer

SQL Server JSON: error converting data type nvarchar to decimal... sometimes

I'm having a weird problem. I have the following SQL statement trying to parse a JSON file: SELECT A.subscription_id AS subscriptionid, A.customer_id AS customerid, A.customer_domain AS customerdomain, A.mpn_id …
1
vote
1 answer

Stored procedure with multiple insert

I want a value of #temp table and People table value together. I can use json_modify but it will work only for single value insert. But in case of multiple values, we can make both in JSON object which will be array of object and combine both. But…
1
vote
1 answer

Use custom string value in T-SQL OPENJSON With clause

DECLARE @json NVARCHAR(4000) = N'{ "pets" : { "cats" : [ { "id" : 1, "name" : "Fluffy", "sex" : "Female" }, { "id" : 2, "name" : "Long Tail", "sex" : "Female" }, { "id" :…
Yoav24
  • 316
  • 4
  • 16
1
vote
1 answer

count values in json array per ID on multiple rows w/ OPENJSON

I have a table containing Id and JsonData columns (table has 100's of rows) JsonData contains an array with various contact ID's for each…
im-devops
  • 27
  • 4
1 2
3
13 14