0

Here is my code:

DECLARE @Orders VARCHAR(MAX)

select @Orders = BulkColumn FROM OPENROWSET(BULK'C:\temp\amazonjson.json.txt', SINGLE_BLOB) JSON;

SELECT *
FROM OPENJSON(@Orders,'$.orders')
WITH(
orderId bigint '$.orderId',
orderNumber nvarchar(50) '$.orderNumber',
orderKey nvarchar(50) '$.orderKey',
orderDate nvarchar(50) '$.orderDate',
paymentDate nvarchar(50) '$.paymentDate',
shipByDate nvarchar(50) '$.shipByDate',
orderStatus nvarchar(50),
customerId nvarchar(50),
customerUsername nvarchar(50),
customerEmail nvarchar(50),
orderTotal nvarchar(50),
amountPaid nvarchar(50),
taxAmount nvarchar(50),
shippingAmount nvarchar(50),
customerNotes nvarchar(50),
internalNotes nvarchar(50),
gift nvarchar(50),
giftMessage nvarchar(50),
paymentMethod nvarchar(50),
requestedShippingService nvarchar(50),
carrierCode nvarchar(50),
serviceCode nvarchar(50),
packageCode nvarchar(50),
confirmation nvarchar(50),
shipDate nvarchar(50),
holdUntilDate nvarchar(50),
billTo nvarchar(max) '$.billTo',
items nvarchar(max)

)

The issue is the last two columns -- billTo is an object -- {"name":"bob smith","address":"1234 main"} -- and items is an array. I was trying to simply display the column as a json string so I can parse it later.

1 Answer 1

1

In order to maintain the JSON string use AS JSON instead of a JSON path:

DECLARE @Orders VARCHAR(MAX)

select @Orders = BulkColumn FROM OPENROWSET(BULK'C:\temp\amazonjson.json.txt', SINGLE_BLOB) JSON;

SELECT *
FROM OPENJSON(@Orders,'$.orders')
WITH(
orderId bigint '$.orderId',
orderNumber nvarchar(50) '$.orderNumber',
orderKey nvarchar(50) '$.orderKey',
orderDate nvarchar(50) '$.orderDate',
paymentDate nvarchar(50) '$.paymentDate',
shipByDate nvarchar(50) '$.shipByDate',
...
billTo nvarchar(max) AS JSON,
items nvarchar(max) AS JSON
Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.