I have a table in a json file (C:\db.json):
[{"_id":"81743561","_record_number":"1","_form_id":"618213","_user_id":"218754",...},
{"_id":"81782299","_record_number":"2","_form_id":"618213","_user_id":"218754",...},
{"_id":"81784481","_record_number":"3","_form_id":"618213","_user_id":"218754",...}]
It has about 60 "columns". I have a 100 "tables" like this to import into SQL Server, each with its own schema. I found it could be done this way:
Declare @JSON varchar(max)
SELECT @JSON=BulkColumn
FROM OPENROWSET (BULK 'C:\db.json', SINGLE_CLOB) import
SELECT *
FROM OPENJSON (@JSON)
WITH
(
_id integer,
_record_number integer,
_form_id integer,
_user_id integer,
...
)
I really need to avoid manually writing the list of columns. One option is to parse the json and generate the column list. Before I start going down that road, I was wondering if there is a way to avoid listing the columns, something equivalent to a SELECT *
?
OPENJSON
with default schema (without theWITH
clause) returns one row for eachkey: value
pair for each object in the JSON array. And the return values arevarchar\nvarchar
. What are the expected data type for the results fromOPENJSON
call - always integer? And what is your SQL Server version? – Phenica