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

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


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20191125/dccac3b5/attachment.html>


More information about the Qgis-user mailing list