Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

json - SQL OPENJSON is not returning the values from sub-arrays

I have a JSON file that is properly formatted according to the Microsoft ISJSON function. However, it refuses to return a value from the nested array.

Here is an excerpt from the JSON file.
I want to return the following fields: id, symbol, name, and price. I can get the first three, but the price always shows up null in the SQL query results.

JSON FILE SNIPPET:

    {
     "status": {
       "timestamp": "2021-01-06T07:14:42.132Z",
       "error_code": 0,
       "error_message": null,
       "elapsed": 14,
       "credit_count": 1,
       "notice": null,
       "total_count": 4180
     },
     "data": [
       {
         "id": 1,
         "name": "Bitcoin",
         "symbol": "BTC",
         "slug": "bitcoin",
         "num_market_pairs": 9772,
         "date_added": "2013-04-28T00:00:00.000Z",
         "tags": [
           "mineable",
           "pow",
           "sha-256",
           "store-of-value",
           "state-channels"
         ],
         "max_supply": 21000000,
         "circulating_supply": 18592156,
         "total_supply": 18592156,
         "platform": null,
         "cmc_rank": 1,
         "last_updated": "2021-01-06T07:13:02.000Z",
         "quote": {
           "USD": {
             "price": 36248.609255662224,
             "volume_24h": 225452557837159.16,
             "percent_change_1h": 2.74047145,
             "percent_change_24h": 19.54362963,
             "percent_change_7d": 29.31750604,
             "market_cap": 673939798064.3159,
             "last_updated": "2021-01-06T07:13:02.000Z"
           }
         }
       }

Here is the SQL Query that I'm using:

DECLARE @JSON VARCHAR(MAX)

SELECT @JSON = BulkColumn
FROM OPENROWSET 
(BULK 'C:TSPoutput.json', SINGLE_CLOB) AS j

Select iif(ISJSON(@JSON)=1,'YES','NO') JSON_OK


Select * FROM OPENJSON (@JSON, '$.data')
    WITH (
        id              int 
       ,symbol          varchar(20) 
       ,[name]          varchar(50)
      ,price            float '$.data.quote.USD[0]'
    )

I've tried everything I can think of to get the price to appear, but I'm missing something as it's not cooperating. Also, I set the database compatibility level to 130 as I read that could be the problem.... Still no luck.

Any help would be much appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$.data.quote.USD is not an array, it's a set of properties. It's also already inside the $.data context so should not include data in its path. Try the following instead:

select *
from openjson(@JSON, '$.data') with
(
    id      int
    ,symbol varchar(20)
    ,[name] varchar(50)
    ,price  float '$.quote.USD.price'
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...