-1

I have json data that I am trying to import in to a SQL Server database but it doesnt have path names to reference - how do I go by indentifying the different elements here:

Sample json: https://api.cryptowat.ch/markets/binance/btcusdt/ohlc?periods=3600

This outputs in the following format but without the names:

[
  CloseTime,
  OpenPrice,
  HighPrice,
  LowPrice,
  ClosePrice,
  Volume,
  QuoteVolume
]
[
    1474736400,
    8744,
    8756.1,
    8710,
    8753.5,
    91.58314308,
    799449.488966417
],

Following this: https://learn.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql?view=sql-server-ver15

But not sure what to put in here: JSON_VALUE ( expression , path ) as the path bit - any help appreciated

Thanks!

optionone
  • 3
  • 4
  • 2
    That isn't valid JSON, you can't use the JSON operations to query something that isn't valid. The "sample" JSON you link to, however, *is* valid JSON, and isn't what you have in your question. – Thom A Jan 10 '21 at 22:15
  • what I have in my question is a copy from the documentation related to that sample; https://docs.cryptowat.ch/rest-api/markets/ohlc. If its not valid json then what is it? any easy way you might know of, or function that would help me get it in to a SQL server db? – optionone Jan 10 '21 at 22:26
  • It's an array, delimited by commas. So you could split the data if you treat it as like a CSV. – Thom A Jan 10 '21 at 22:29
  • I want to create a job that proceses that data and continually imports so need to process it all in SQL server. Was using this: https://github.com/geral2/SQL-APIConsumer/blob/master/README.md but it falls over as far as I can see without being able to define the individual elements – optionone Jan 10 '21 at 22:32
  • I feel like what you're saying here is this is an [XY Problem](http://xyproblem.info). – Thom A Jan 10 '21 at 22:36
  • yes - i know what I need as an output, have not much of a clue what to do to describe this properly or what to look for to sort this, so was hoping for some guidance on here. appreciate your help though – optionone Jan 10 '21 at 22:40

1 Answers1

0

That appears to be a bunch of arrays representing rows. A query like this should do it:

declare @json nvarchar(max) = '
{"result":{"3600":[[1606705200,18482.31,18550,18435.81,18506.87,2470.427799,45664903.49372367],[1606708800,18506.87,18593.81, ...
'

select dateadd(second,cast(json_value([value],'$[0]') as bigint),'19700101') CloseTime,
       cast(json_value([value],'$[1]') as float) OpenPrice,
       cast(json_value([value],'$[2]') as float) HighPrice,
       cast(json_value([value],'$[3]') as float) LowPrice,
       cast(json_value([value],'$[4]') as float) ClosePrice,
       cast(json_value([value],'$[5]') as float) Volume,
       cast(json_value([value],'$[6]') as float) QuoteVolume

from openjson(@json,'$.result."3600"') 

which outputs

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
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67