Following this article I'm trying to implement a query that could retrieve data from a REST call and parse the JSON with OPENJSON.
I call this URL with Postman to check if the data are alright:
The data are there.
I copy paste the JSON and I test my call on SSMS:
DECLARE @json nvarchar(max) = N'{
"version": 0.6,
"generator": "Overpass API 0.7.55.4 3079d8ea",
"osm3s": {
"timestamp_osm_base": "2018-10-04T10:07:02Z",
"timestamp_areas_base": "2018-10-04T09:02:02Z",
"copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
},
"elements": [
{
"type": "node",
"id": 501591237,
"lat": 49.4943882,
"lon": -117.2910343,
"tags": {
"amenity": "cinema",
"name": "Civic Theatre",
"website": "http://civictheatre.ca"
}
},
{
"type": "node",
"id": 2293276829,
"lat": -41.2709865,
"lon": 173.2842196,
"tags": {
"amenity": "cinema",
"name": "Cinema 6"
}
}
]
}';
SELECT * FROM OPENJSON(@json, N'$.elements')
WITH (
[type] nvarchar(max) N'$.type' ,
[id] nvarchar(max) N'$.id',
[lat] nvarchar(max) N'$.lat',
[lon] nvarchar(max) N'$.lon',
[amenity] nvarchar(max) N'$.tags.amenity',
[name] nvarchar(max) N'$.tags.name'
)
All good, is working: my query can parse the JSON.
I now follow this guide that shows me how to make a REST call within SSMS and I try to retrieve the data of the same URL and see them on-the-fly into SSMS:
Declare @Object as Int;
Declare @json as nvarchar(max);
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',
'http://overpass-api.de/api/interpreter?data=[out:json];area[name=%22Nelson%22]-%3E.a;(node(area.a)[amenity=cinema];way(area.a)[amenity=cinema];rel(area.a)[amenity=cinema];);out;', --Your Web Service Url (invoked)
'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @json OUTPUT
select @json
SELECT * FROM OPENJSON(@json, N'$.elements')
WITH (
[type] nvarchar(max) N'$.type' ,
[id] nvarchar(max) N'$.id',
[lat] nvarchar(max) N'$.lat',
[lon] nvarchar(max) N'$.lon',
[amenity] nvarchar(max) N'$.tags.amenity',
[name] nvarchar(max) N'$.tags.name'
)
Exec sp_OADestroy @Object
Ouch! Is not working!
Why the data are empty? Declare @json as nvarchar(max);
was working in the previous query. why is not working any more?
If I put Declare @json as varchar(8000);
it works only because I'm retrieving less than 8000 characters but I cannot work this way.
If I'm not wrong OPENJSON
was developed exactly to avoid such problem and load till 2GB of data.
I want to use nvarchar(max)
and load a lot of data. This is the whole point of using OPENJSON
.