What is import JSON? Examples

What is import JSON?

Python load JSON. JSON may be a syntax for storing and exchanging information.
JSON is text, written with JavaScript object notation.

JSON in Python

Python incorporates a constitutional package known as JSON, which may be wont to work with JSON information.

Example

import the JSON module:

import json

Parse JSON – Convert from JSON to Python. If you’ve got a JSON string, you’ll analyze it by exploiting the json.loads() methodology.
The result is going to be a Python wordbook.

Convert from JSON to Python:

import json

# some JSON example:
detail = '{ "name":"John", "age":30, "city":"New York"}'

# parse detail:
result = json.loads(detail)

# the result is a Python dictionary:
print(result["age"])

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
30

Process finished with exit code 0

Convert from Python to JSON

If you’ve got a Python object, you’ll convert it into a JSON string by exploiting the json.dumps() methodology.

Example

import json

# a Python object (dict):

x = {"age": 30,"city": "New York"}

# convert into JSON:

y = json.dumps(x)

# the result's a JSON string:

print(y)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
{"age": 30, "city": "New York"}

Process finished with exit code 0

You will convert Python objects of the subsequent varieties, into JSON strings:

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

Convert Python objects into JSON strings, and print the values:

import json

print(json.dumps(["pineapple", "bananas"]))
print(json.dumps(("Volleyball", "Football")))
print(json.dumps("hello"))
print(json.dumps(45))
print(json.dumps(45.12))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
["pineapple", "bananas"]
["Volleyball", "Football"]
"hello"
45
45.12
true
false
null

Process finished with exit code 0

When you convert from Python to JSON, Python objects are born again into the JSON (JavaScript) equivalent:

PythonJSON
dictObject
listArray
tupleArray
strString
intNumber
floatNumber
Truetrue
Falsefalse
Nonenull

h

Convert a Python object containing all the legal information types:

import json

detail = {
"name": "Mickey",
"age": 35,
"married": True,
"divorced": False,
"children": ("Anna","Jhon"),
"pets": None,
"cars": [
{"model": "BMW 230", "mpg": 30.4},
{"model": "Ford Edge", "mpg": 21.5}
]
}

print(json.dumps(detail))

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
{"name": "Mickey", "age": 35, "married": true, "divorced": false, "children": ["Anna", "Jhon"], "pets": null, "cars": [{"model": "BMW 230", "mpg": 30.4}, {"model": "Ford Edge", "mpg": 21.5}]}

Process finished with exit code 0

Format the Result:

The example on top of prints a JSON string, however, it’s not terribly straightforward to browse, with no indentations and line breaks.

The json.dumps() methodology has parameters to create it easier to browse the result:

Use the indent parameter to outline the numbers of indents:

json.dumps(detail, indent=4)

You can conjointly outline the separators, default worth is (“, “, “: “), which implies employing a comma and an area to separate every object, and a colon and an area to separate keys from values:

Use the separators parameter to vary the default separator:

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

Order the Result

The json.dumps() methodology has parameters to order the keys within the result:

Example:

Use the sort_keys parameter to specify if the result ought to be sorted or not:

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

Recommended Post:

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



0 thoughts on “What is import JSON? Examples”

Leave a Comment