1

I try to get data from JSON with stored procedures and I have data in JSON format.

This is my data:

{
  "data": [
    {
      "@attributes": {
        "id": "123456",
        "name": "test"
      }
    },
    {
      "prices": {
        "price": [
          {
            "@attributes": [
              {
                "date": "2019-06-13",
                "price": "600",
                "currency": "$"
              },
              {
                "date": "2019-06-15",
                "price": "700",
                "currency": "$"
              }
            ]
          }
        ]
      }
    },
    {
      "images": {
        "image": [
          {
            "@attributes": [
              {
                "date": "2019-06-13",
                "url": "xxxxx"
              }
            ]
          }
        ]
      }
    }
  ]
}

and I try will get data price in JSON by get "id": "123456" is PK of price

This my stored procedure:

INSERT @price_tmp (id, date, price, currency) 
    SELECT id, date, price, currency 
    FROM OPENJSON(@InputJson,'$.data.prices.price')
            WITH (
                  id nvarchar(50) '../../$."@attributes".id',
                  price_date nvarchar(255) '$."@attributes".date', 
                  price_price nvarchar(50) '$."@attributes".price', 
                  price_currency nvarchar(255) '$."@attributes".currency'
                 ) AS jsonValues`

after I do

select * from @price_tmp;

I expect to see data like this:

ID     |   date     |  price  | currency
-------+------------+---------+---------
123456 | 2019-06-13 |  600    |       $
123456 | 2019-06-15 |  700    |       $

But it does not show anything

I can use Answer both

https://stackoverflow.com/a/57967971/8556614

https://stackoverflow.com/a/57967997/8556614

Thank you metal and Zhorov

2 Answers 2

1

it seems your json is not anymore the correct one based on your query. consider changing how you parse your json data.

INSERT @price_tmp (id,date,price,currency) 
SELECT t1.id, t1.price_date, t1.price_price, t1.price_currency FROM (
    SELECT JSON_Value (c.[value], '$."@attributes".id') as id 
        ,  jsonValues.price_date
        ,  jsonValues.price_price
        ,  jsonValues.price_currency
    FROM OPENJSON(@InputJson,'$.data[1].prices.price[0]."@attributes"')
    WITH (  
        price_date nvarchar(255) '$.date', 
        price_price nvarchar(50) '$.price', 
        price_currency nvarchar(255) '$.currency'
    ) AS jsonValues
    CROSS APPLY OPENJSON(@InputJson, '$.data') c) t1
WHERE t1.id is not null
Sign up to request clarification or add additional context in comments.

7 Comments

In SELECT t1.id, t1.price_date, t1.price_price, t1.price_currency I will DECLARE ?
@storyks no need to declare. replace your entire insert script. this should do it.
It no data after I select*from @price_tmp
your @price_tmp is temporary table, you need to create it first
Yes, I create @price_tmp before insert
|
1

This is a complex JSON data, but if the structure of this JSON is fixed, then the next approach may help. You need to use JSON_VALUE() to get the id and OPENJSON() with explicit schema definition to get the prices data.

JSON:

DECLARE @InputJson nvarchar(max) = N'{
  "data": [
    {
      "@attributes": {
        "id": "123456",
        "name": "test"
      }
    },
    {
      "prices": {
        "price": [
          {
            "@attributes": [
              {
                "date": "2019-06-13",
                "price": "600",
                "currency": "$"
              },
              {
                "date": "2019-06-15",
                "price": "700",
                "currency": "$"
              }
            ]
          }
        ]
      }
    },
    {
      "images": {
        "image": [
          {
            "@attributes": [
              {
                "date": "2019-06-13",
                "url": "xxxxx"
              }
            ]
          }
        ]
      }
    }
  ]
}'

Statement:

DECLARE @price_tmp TABLE(
   id nvarchar(50),
   price_date nvarchar(255), 
   price_price nvarchar(50), 
   price_currency nvarchar(255)
)   

INSERT INTO @price_tmp   
SELECT 
   JSON_VALUE(@InputJson, '$.data[0]."@attributes".id') AS ID,
   j.*
FROM OPENJSON(@InputJson, '$.data[1].prices.price[0]."@attributes"') WITH (
   price_date nvarchar(255) '$.date', 
   price_price nvarchar(50) '$.price', 
   price_currency nvarchar(255) '$.currency'
) j

SELECT *
FROM @price_tmp

Output:

------------------------------------------
ID      price_date  price_price price_currency
------------------------------------------
123456  2019-06-13  600         $
123456  2019-06-15  700         $

2 Comments

It no data after I select*from @price_tmp
i use EXEC Sync_Resource_test @InputJson = JSON from My question it error Msg 13607, Level 16, State 3, Procedure Sync_Resource_test, Line 317 [Batch Start Line 0] The format of the JSON path is not correct. An unexpected character ". Is at position 0.

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.