I have a have a static URL pointing to a json file on the web that I load to QGIS. It is updated regularly. In the source, before listing the features, it contains metadata, between else a line "creation_time": "[timestamp]".

I want to add the date/time as text to the print layout: how do I get the information? I tried to get it using expressions with layer_property( '[layer_name]', 'attribution'), but the output is empty.

The first few lines of the .json file look like this:

"crs": {
"license": "[license info]",
"mapname": "[name of the map]",
"map_long_name": "[despription of the map]",
"map_short_name": "[short name]",
"map_abstract": "[abstract of the map]",
"creation_time": "[timestamp]",

enter image description here

1 Answer 1


You could use json_to_map() to turn your json into a dictionary and then get the value of 'creation_time' by using map_get() to search for that key. Here an example, (I made a full json out of your example, copying a random one from the web):

   "license":"[license info]",
   "mapname":"[name of the map]",
   "map_long_name":"[despription of the map]",
   "map_short_name":"[short name]",
   "map_abstract":"[abstract of the map]",
            "name":"codecentric AG",
            "address":"Hochstr. 11",

This example will return '[timestamp]'. Depending on how you access this data, you need to hand it over as string to this function.

I could not find a possibility to load data from external urls via expressions. So I created this custom function:

from qgis.core import *
from qgis.gui import *
import urllib.request
import urllib
import json

@qgsfunction(args='auto', group='Custom')
def jsonfromurl(url, feature, parent):
    header = {"accept":"application/json"}
    request = urllib.request.Request(url)
    response = urllib.request.urlopen(request)
    response_data = response.read()
    encoding = response.info().get_content_charset('utf-8')
    data = json.loads(response_data.decode(encoding))
    #txt = json.dumps(data) #uncomment this line to receive data as string. also change next line to: return txt
    return data

It basically accesses the json content from a given url. My test url is http://api.plos.org/search?q=title:DNA. In case your url does not return a json by default, you may need adjust the second line to request = urllib.request.Request(url,header). In case you want to receive your json as string, uncomment the second last line and change the last one to return txt.

Example usage: map_get(jsonfromurl('http://api.plos.org/search?q=title:DNA'),'response'), so in your case maybe map_get(jsonfromurl('yoururls'),'creation_time').

  • OK, so this implies to (manually) copying the data to the expression. As I download this file from an URL (it's regularly changing with updated data), I hoped to automatize this
    – Babel
    Commented Jan 29, 2021 at 23:11
  • 1
    Well, thats the basic idea I got from your question. Maybe include this part as well. So you have a dynamic(?) or static(?) url pointing to a json on the web(?) or local(?), you then want to read? Or is it just a field in a layer?
    – MrXsquared
    Commented Jan 29, 2021 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.