Python JSON

JSON is een syntaxis gebruikt voor het opslaan en uitwisselen van gegevens.

JSON is een tekst geschreven in JavaScript Object Notation (JavaScript object notation).

JSON in Python

Python heeft een genaamd json van de ingebouwde pakketten, die kunnen worden gebruikt om JSON-gegevens te verwerken.

Example

importeren json module:

import json

JSON parseren - JSON omzetten naar Python

Als er een JSON-tekst is, kan men json.loads() met deze methode kan je het analyseren.

het resultaat zal een Python-dictionary zijn.

Example

Converteer JSON naar Python:

import json
# Enkele JSON:
x = '{ "name":"Bill", "leeftijd":63, "stad":"Seatle"}'
# Parseer x:
y = json.loads(x)
# Resultaat is een Python-dictionary:
print(y["leeftijd"])

Run Example

Converteer Python naar JSON

Als er een Python-object is, kan je json.dumps() met deze methode om het naar een JSON-string te converteren.

Example

Converteer Python naar JSON:

import json
# Python-object (dictionair):
x = {
  "naam": "Bill",
  "leeftijd": 63,
  "stad": "Seatle"
}
# Converteer naar JSON:
y = json.dumps(x)
# Resultaat is een JSON-string:
print(y)

Run Example

Je kunt de volgende types van Python-objecten naar JSON-string converteren:

  • dict
  • list
  • tuple
  • string
  • int
  • float
  • True
  • False
  • None

Example

Converteer een Python-object naar een JSON-string en print de waarde:

import json
print(json.dumps({"name": "Bill", "age": 63}))
print(json.dumps(["apple", "bananas"]))
print(json.dumps(("apple", "bananas")))
print(json.dumps("hello"))
print(json.dumps(42))
print(json.dumps(31.76))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))

Run Example

Bij het conversie van Python naar JSON worden Python-objects geconverteerd naar JSON (JavaScript) equivalente items:

Python JSON
dict Object
list Array
tuple Array
str String
int Number
float Number
True true
False false
None null

Example

Converteer een Python-object dat alle geldige datatypes bevat:

import json
x = {
  "naam": "Bill",
  "leeftijd": 63,
  "getenhuwd": True,
  "divorced": False,
  "children": ("Jennifer","Rory","Phoebe"),
  "pets": None,
  "cars": [
    {"model": "Porsche", "mpg": 38.2},
    {"model": "BMW M5", "mpg": 26.9}
  }
}
print(json.dumps(x))

Run Example

Format the results

The above example prints a JSON string, but it is not easy to read without indentation and line breaks.

json.dumps() The method provides parameters to make the results more readable:

Example

Use indent Parameter defines the indentation number:

json.dumps(x, indent=4)

Run Example

You can also define separators, the default value is (", ", ": "), which means that each object is separated by a comma and a space, and keys and values are separated by a colon and a space:

Example

Use separators Parameter to change the default separators:

json.dumps(x, indent=4, separators=(". ", " = "))

Run Example

Sort the results

json.dumps() The method provides parameters to sort the keys in the results:

Example

Use sort_keys Parameter to specify whether the results should be sorted:

json.dumps(x, indent=4, sort_keys=True)

Run Example