问题描述
我有要导入 sql Server 数据库的 json 数据,但它没有要引用的路径名 - 我如何通过在此处识别不同元素来进行:
示例 json:https://api.cryptowat.ch/markets/binance/btcusdt/ohlc?periods=3600
[
CloseTime,OpenPrice,HighPrice,LowPrice,ClosePrice,Volume,QuoteVolume
]
[
1474736400,8744,8756.1,8710,8753.5,91.58314308,799449.488966417
],
接下来:https://docs.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql?view=sql-server-ver15
但不确定在这里放什么: JSON_VALUE ( expression,path ) 作为路径位 - 感谢任何帮助
谢谢!
解决方法
这似乎是一堆代表行的数组。像这样的查询应该这样做:
declare @json nvarchar(max) = '
{"result":{"3600":[[1606705200,18482.31,18550,18435.81,18506.87,2470.427799,45664903.49372367],[1606708800,18593.81,...
'
select dateadd(second,cast(json_value([value],'$[0]') as bigint),'19700101') CloseTime,'$[1]') as float) OpenPrice,'$[2]') as float) HighPrice,'$[3]') as float) LowPrice,'$[4]') as float) ClosePrice,'$[5]') as float) Volume,'$[6]') as float) QuoteVolume
from openjson(@json,'$.result."3600"')
哪个输出
CloseTime OpenPrice HighPrice LowPrice ClosePrice Volume QuoteVolume
----------------------- ---------------------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
2020-11-30 03:00:00.000 18482.31 18550 18435.81 18506.87 2470.427799 45664903.4937237
2020-11-30 04:00:00.000 18506.87 18593.81 18431.26 18579.5 2283.371404 42288794.16279
2020-11-30 05:00:00.000 18579.67 18619.79 18450 18523.79 2119.718173 39247661.0930612
2020-11-30 06:00:00.000 18523.79 18627.29 18477.62 18520.47 1835.535407 34068066.5616724
2020-11-30 08:00:00.000 18520.27 18677 18512.9 18623 3499.555499 65178082.9264658
2020-11-30 09:00:00.000 18623.05 18625 18375.55 18462.31 4220.609403 77972731.2494644
2020-11-30 10:00:00.000 18462.7 18510 18326.03 18478.54 3054.085606 56248649.2319086
2020-11-30 11:00:00.000 18478.54 18589 18446.5 18503.06 2240.917883 41516090.5672204
2020-11-30 12:00:00.000 18503.11 18655 18503.11 18600.95 3413.482688 63562162.7584654
2020-11-30 13:00:00.000 18600.94 18840 18600.94 18837.16 4675.208754 87625034.6027069
2020-11-30 14:00:00.000 18835.97 19210 18788.11 19151.49 9286.987366 176378855.887698
2020-11-30 15:00:00.000 19151.49 19782.1 19135 19480.8 15792.38031 307214715.045745