[Qgis-user] Loading non-spatial JSON using Python

Cliff Patterson cpatterson at psdrcs.com
Mon Nov 25 10:07:25 PST 2019


Thanks! I figured some sort of conversion would be necessary. I'll give
that a go.

Cheers,
Cliff

On Mon, Nov 25, 2019 at 12:15 PM Alessandro Pasotti <apasotti at gmail.com>
wrote:

> Hi,
>
> that doesn't look valid GeoJSON to me, you have no other options than
> transforming  the JSON to valid GeoJSON (or to any other format recognized
> by QGIS ogr provider), or you can create a vector layer from scratches and
> and add the individual features in a loop, it largely depends on what you
> need to do next with those data.
>
> Both approaches require a bit of programming.
>
>
>
> On Mon, Nov 25, 2019 at 6:10 PM Cliff Patterson <cpatterson at psdrcs.com>
> wrote:
>
>> I have a JSON endpoint that I want to load into a QGIS project as a flat
>> table (no geometry). There are lat/ling coordinates in JSON (see below),
>> but those coords are not important for my purpose since I want to
>> eventually join the table with a spatial layer using a common ID. I can
>> load the data into the console with:
>>
>> import urllib.request, json, csv
>> with urllib.request.urlopen("https://my_json_endpoint") as url:
>>     data = json.loads(url.read().decode())
>>     print(data)
>>
>> And I can load the table into QGIS as a GeoJSON with:
>>
>> import urllib.request, json
>> uri = 'https://my_json_endpoint'
>> iface.addVectorLayer(uri, 'Asset Data', 'ogr')
>>
>> but the table loads without attribute data and no rows.
>>
>> The JSON file looks like this (but with 20+ rows of data)
>>
>> {
>>     "Assets": [
>>         {
>>             "adjusted_cost": 1940993,
>>             "adjusted_eul": 240,
>>             "adjusted_quantity": 1,
>>             "amort_date": "2039-11-01",
>>             "attributes": [],
>>             "classifications": {
>>                 "Acc. Amort. Code": "No Acc. Amort. Code",
>>                 "Amort. Exp. Code": "No Amort. Exp. Code",
>>                 "Capital Code": "No Capital Code",
>>                 "Category": "Roads",
>>                 "Class": "Infrastructure",
>>                 "Department": "No Department",
>>                 "Function": "No Function",
>>                 "Schedule 51B Code": "No Schedule 51B Code",
>>                 "Schedule 75C Code": "No Schedule 75C Code",
>>                 "Segment": "Surface",
>>                 "Sub-Function": "No Sub-Function"
>>             },
>>             "condition": 100,
>>             "condition_assessment_date": "Age-based",
>>             "condition_ratio": 1,
>>             "description": null,
>>             "eul": 240,
>>             "gis_feature_id": "roads.1",
>>             "historical_cost": 1940993,
>>             "id": 1,
>>             "import_id": "PATCH-1",
>>             "initial_quantity": 1,
>>             "is_fully_disposed": false,
>>             "is_fully_transferred": false,
>>             "is_lifecycle_excluded": true,
>>             "lat": 42.985708676898,
>>             "lifecycle_eul": 240,
>>             "lifecycle_eul_events": 240,
>>             "location": null,
>>             "lon": -81.254337208244,
>>             "name": "Road name 1",
>>             "profile": "Default Profile",
>>             "replacement_cost": 0,
>>             "replacement_date": "2039-11-01",
>>             "risk_consequence": 5,
>>             "risk_probability": 1,
>>             "risk_rating": 5,
>>             "service_date": "2019-11-12",
>>             "unit_of_measure": "Area (m2)"
>>         },
>>
>>          ...
>>
>> }
>>
>>
>> I could write a script to load the data into a DB table dynamically, but it would be much better to load the data dynamically in QGIS. The ultimate objective would be to create a tool that loads the data for the user.
>>
>> Any ideas how I can achieve this?
>>
>> Thanks!
>>
>> Cliff
>>
>> _______________________________________________
>> Qgis-user mailing list
>> Qgis-user at lists.osgeo.org
>> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
>
>
> --
> Alessandro Pasotti
> w3:   www.itopen.it
>


-- 

Cliff Patterson Ph.D.

*PSD* | Senior GIS Consultant
P: 519-690-2565 ext. 2616
www.psdrcs.com
London | 148 Fullarton St. 9th Floor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20191125/26048542/attachment.html>


More information about the Qgis-user mailing list