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:
Python | JSON |
dict | Object |
list | Array |
tuple | Array |
str | String |
int | Number |
float | Number |
True | true |
False | false |
None | null |
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:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number