Search code examples
pythonjsonjson-deserialization

python: json print root object names


I have a JSON String like this:

{
  "Sommersprossen": {
    "count": 5,
    "lastSeenTime": 1586959168567
  },
  "inkognito": {
    "count": 7,
    "lastSeenTime": 1586901586361
  },
  "Marienkäfer": {
    "count": 7,
    "lastSeenTime": 1586958264090,
    "difficulty": 0.8902439024390244,
    "difficultyWeight": 82
  },
  "Zaun": {
    "count": 8,
    "lastSeenTime": 1586958848320
  },

Now I want to print something like this: Sommersprossen, inkognito, Marienkäfer, Zaun.
But I don't know how to... My existing Python code is:

url = "https://skribbliohints.github.io/German.json"

response = urllib.request.urlopen(url)

data = json.loads(response.read())

But the problem is I cant find a way to print the objects names.


Solution

  • A json is essentially just a dictionary. You can access it like any other python dictionary.

    url = "https://skribbliohints.github.io/German.json"
    
    response = urllib.request.urlopen(url)
    
    data = json.loads(response.read())
    
    roots = list(data.keys())