Json Decode Error When Reading Json Data
What is JSON in Python?
JSON in Python is a standard format inspired by JavaScript for data commutation and data transfer equally text format over a network. Generally, JSON is in string or text format. It can exist used past APIs and databases, and it represents objects every bit name/value pairs. JSON stands for JavaScript Object Notation.
Python JSON Syntax:
JSON is written as fundamental and value pair.
{ "Key": "Value", "Primal": "Value", }
JSON is very similar to Python dictionary. Python supports JSON, and it has an inbuilt library as a JSON.
JSON Library in Python
'marshal' and 'pickle' external modules of Python maintain a version of JSON Python library. Working with JSON in Python to perform JSON related operations like encoding and decoding, you need to get-go import JSON library and for that in your .py file,
import json
Following methods are available in the JSON Python module
Method | Clarification |
---|---|
dumps() | encoding to JSON objects |
dump() | encoded string writing on file |
loads() | Decode the JSON string |
load() | Decode while JSON file read |
Python to JSON (Encoding)
JSON Library of Python performs following translation of Python objects into JSON objects by default
Python | JSON |
---|---|
dict | Object |
listing | Array |
unicode | String |
number – int, long | number – int |
bladder | number – existent |
True | Truthful |
Fake | False |
None | Null |
Converting Python data to JSON is called an Encoding operation. Encoding is washed with the help of JSON library method – dumps()
JSON dumps() in Python
json.dumps() in Python is a method that converts dictionary objects of Python into JSON string data format. It is useful when the objects are required to be in string format for the operations like parsing, printing, etc.
At present lets perform our first json.dumps encoding instance with Python:
import json 10 = { "name": "Ken", "age": 45, "married": True, "children": ("Alice","Bob"), "pets": ['Dog'], "cars": [ {"model": "Audi A1", "mpg": 15.one}, {"model": "Zeep Compass", "mpg": eighteen.1} ] } # sorting upshot in asscending order by keys: sorted_string = json.dumps(x, indent=4, sort_keys=True) impress(sorted_string)
Output:
{"person": {"name": "Kenn", "sex activity": "male", "age": 28}})
Let's see an case of Python write JSON to file for creating a JSON file of the dictionary using the same function dump()
# here nosotros create new data_file.json file with write way using file i/o operation with open('json_file.json', "w") as file_write: # write json data into file json.dump(person_data, file_write)
Output:
Nothing to testify…In your organization json_file.json is created. You can check that file as shown in the below write JSON to file Python example.
JSON to Python (Decoding)
JSON string decoding is done with the assistance of inbuilt method json.loads() & json.load() of JSON library in Python. Here translation table show instance of JSON objects to Python objects which are helpful to perform decoding in Python of JSON string.
JSON | Python |
---|---|
Object | dict |
Assortment | listing |
String | unicode |
number – int | number – int, long |
number – real | float |
Truthful | True |
Simulated | False |
Null | None |
Permit's run across a basic parse JSON Python example of decoding with the help of json.loads function,
import json # json library imported # json information string person_data = '{ "person": { "name": "Kenn", "sexual activity": "male", "age": 28}}' # Decoding or converting JSON format in dictionary using loads() dict_obj = json.loads(person_data) print(dict_obj) # check blazon of dict_obj print("Blazon of dict_obj", type(dict_obj)) # go human object details print("Person......", dict_obj.get('person'))
Output:
{'person': {'name': 'Kenn', 'sex': 'male', 'age': 28}} Type of dict_obj <class 'dict'> Person...... {'name': 'John', 'sex': 'male person'}
Decoding JSON File or Parsing JSON file in Python
At present, we will larn how to read JSON file in Python with Python parse JSON example:
NOTE: Decoding JSON file is File Input /Output (I/O) related operation. The JSON file must exist on your system at specified the location that yous mention in your program.
Python read JSON file Example:
import json #File I/O Open office for read data from JSON File with open up('X:/json_file.json') as file_object: # shop file data in object data = json.load(file_object) print(data)
Hither information is a dictionary object of Python as shown in the higher up read JSON file Python example.
Output:
{'person': {'name': 'Kenn', 'sex': 'male', 'age': 28}}
Meaty Encoding in Python
When you need to reduce the size of your JSON file, you tin use compact encoding in Python.
Example,
import json # Create a List that contains dictionary lst = ['a', 'b', 'c',{'4': 5, 'six': vii}] # separator used for compact representation of JSON. # Use of ',' to identify list items # Utilise of ':' to identify key and value in lexicon compact_obj = json.dumps(lst, separators=(',', ':')) print(compact_obj)
Output:
'["a", "b", "c", {"4": 5, "6": 7}]' ** Here output of JSON is represented in a single line which is the most compact representation by removing the infinite graphic symbol from compact_obj **
Format JSON code (Pretty print)
- The aim is to write well-formatted lawmaking for human understanding. With the assistance of pretty press, anyone can easily understand the code.
Example:
import json dic = { 'a': iv, 'b': 5 } ''' To format the code utilise of indent and 4 shows number of infinite and use of separator is not necessary simply standard mode to write code of item part. ''' formatted_obj = json.dumps(dic, indent=iv, separators=(',', ': ')) print(formatted_obj)
Output:
{ "a" : 4, "b" : 5 }
To better sympathize this, modify indent to xl and discover the output-
Ordering the JSON code:
sort_keys attribute in Python dumps function'south statement will sort the key in JSON in ascending order. The sort_keys argument is a Boolean attribute. When it's true sorting is allowed otherwise non. Permit's empathise with Python cord to JSON sorting example.
Instance,
import json x = { "proper noun": "Ken", "historic period": 45, "married": Truthful, "children": ("Alice", "Bob"), "pets": [ 'Dog' ], "cars": [ {"model": "Audi A1", "mpg": 15.1}, {"model": "Zeep Compass", "mpg": 18.1} ], } # sorting effect in asscending order by keys: sorted_string = json.dumps(x, indent=4, sort_keys=Truthful) print(sorted_string)
Output:
{ "historic period": 45, "cars": [ { "model": "Audi A1", "mpg": fifteen.i }, { "model": "Zeep Compass", "mpg": 18.1 } ], "children": [ "Alice", "Bob" ], "married": true, "name": "Ken", "pets": [ "Dog" ] }
As y'all may detect the keys age, cars, children, etc are arranged in ascending order.
Complex Object encoding of Python
A Circuitous object has two different parts that is
- Real part
- Imaginary part
Example: iii +2i
Before performing encoding of a complex object, you demand to cheque a variable is circuitous or non. You need to create a function which checks the value stored in a variable by using an example method.
Permit'south create the specific part for check object is complex or eligible for encoding.
import json # create role to check case is circuitous or not def complex_encode(object): # cheque using isinstance method if isinstance(object, complex): return [object.real, object.imag] # raised error using exception handling if object is not complex raise TypeError(repr(object) + " is non JSON serialized") # perform json encoding by passing parameter complex_obj = json.dumps(iv + 5j, default=complex_encode) impress(complex_obj)
Output:
'[4.0, 5.0]'
Complex JSON object decoding in Python
To decode complex object in JSON, use an object_hook parameter which checks JSON string contains the complex object or not. Lets sympathize with cord to JSON Python Example,
import json # function check JSON cord contains complex object def is_complex(objct): if '__complex__' in objct: render circuitous(objct['existent'], objct['img']) render objct # employ of json loads method with object_hook for check object complex or non complex_object =json.loads('{"__complex__": true, "existent": 4, "img": 5}', object_hook = is_complex) #here we not passed complex object then it's convert into dictionary simple_object =json.loads('{"real": vi, "img": 7}', object_hook = is_complex) print("Complex_object......",complex_object) print("Without_complex_object......",simple_object)
Output:
Complex_object...... (4+5j) Without_complex_object...... {'real': vi, 'img': seven}
Overview of JSON Serialization course JSONEncoder
JSONEncoder class is used for serialization of any Python object while performing encoding. Information technology contains iii different methods of encoding which are
- default(o) – Implemented in the subclass and render serialize object for o object.
- encode(o) – Aforementioned every bit JSON dumps Python method return JSON cord of Python information structure.
- iterencode(o) – Represent cord one past one and encode object o.
With the help of encode() method of JSONEncoder class, we can likewise encode whatever Python object as shown in the below Python JSON encoder instance.
# import JSONEncoder course from json from json.encoder import JSONEncoder colour_dict = { "colour": ["reddish", "yellow", "green" ]} # direct chosen encode method of JSON JSONEncoder().encode(colour_dict)
Output:
'{"colour": ["red", "yellow", "green"]}'
Overview of JSON Deserialization class JSONDecoder
JSONDecoder class is used for deserialization of whatsoever Python object while performing decoding. It contains three different methods of decoding which are
- default(o) – Implemented in the subclass and return deserialized object o object.
- decode(o) – Same as json.loads() method return Python data structure of JSON string or data.
- raw_decode(o) – Represent Python lexicon one by one and decode object o.
With the help of decode() method of JSONDecoder form, we can likewise decode JSON string as shown in below Python JSON decoder instance.
import json # import JSONDecoder grade from json from json.decoder import JSONDecoder colour_string = '{ "colour": ["red", "yellow"]}' # directly called decode method of JSON JSONDecoder().decode(colour_string)
Output:
{'colour': ['blood-red', 'yellow']}
Decoding JSON data from URL: Real Life Case
We will fetch information of CityBike NYC (Bike Sharing System) from specified URL(https://feeds.citibikenyc.com/stations/stations.json) and convert into lexicon format.
Python load JSON from file Example:
NOTE:- Brand sure requests library is already installed in your Python, If not so open Terminal or CMD and blazon
- (For Python 3 or in a higher place) pip3 install requests
import json import requests # go JSON string data from CityBike NYC using spider web requests library json_response= requests.go("https://feeds.citibikenyc.com/stations/stations.json") # check type of json_response object print(type(json_response.text)) # load data in loads() part of json library bike_dict = json.loads(json_response.text) #check type of news_dict print(type(bike_dict)) # now become stationBeanList key information from dict print(bike_dict['stationBeanList'][0])
Output:
<class 'str'> <grade 'dict'> { 'id': 487, 'stationName': 'Due east 20 St & FDR Bulldoze', 'availableDocks': 24, 'totalDocks': 34, 'latitude': xl.73314259, 'longitude': -73.97573881, 'statusValue': 'In Service', 'statusKey': one, 'availableBikes': 9, 'stAddress1': 'East twenty St & FDR Drive', 'stAddress2': '', 'city': '', 'postalCode': '', 'location': '', 'altitude': '', 'testStation': Faux, 'lastCommunicationTime': '2018-12-11 x:59:09 PM', 'landMark': '' }
Exceptions Related to JSON Library in Python:
- Course json.JSONDecoderError handles the exception related to decoding operation. and it'due south a subclass of ValueError.
- Exception – json.JSONDecoderError(msg, doc)
- Parameters of Exception are,
- msg – Unformatted Error bulletin
- md – JSON docs parsed
- pos – start index of doc when it's failed
- lineno – line no shows stand for to pos
- colon – column no correspond to pos
Python load JSON from file Instance:
import json #File I/O Open function for read information from JSON File data = {} #Ascertain Empty Dictionary Object try: with open('json_file_name.json') as file_object: information = json.load(file_object) except ValueError: impress("Bad JSON file format, Alter JSON File")
Infinite and NaN Numbers in Python
JSON Data Interchange Format (RFC – Request For Comments) doesn't let Infinite or Nan Value but there is no brake in Python- JSON Library to perform Space and Nan Value related operation. If JSON gets Space and Nan datatype than it's converted it into literal.
Example,
import json # pass float Infinite value infinite_json = json.dumps(float('inf')) # check space json blazon print(infinite_json) print(type(infinite_json)) json_nan = json.dumps(bladder('nan')) print(json_nan) # pass json_string equally Infinity space = json.loads('Infinity') print(infinite) # bank check type of Infinity impress(type(infinite))
Output:
Infinity <class 'str'> NaN inf <class 'float'>
Repeated key in JSON Cord
RFC specifies the key proper name should be unique in a JSON object, only information technology'southward not mandatory. Python JSON library does not raise an exception of repeated objects in JSON. It ignores all repeated key-value pair and considers simply terminal primal-value pair amid them.
- Example,
import json repeat_pair = '{"a": 1, "a": 2, "a": 3}' json.loads(repeat_pair)
Output:
{'a': 3}
CLI (Command Line Interface) with JSON in Python
json.tool provides the command line interface to validate JSON pretty-print syntax. Permit'southward run across an instance of CLI
$ echo '{"name" : "Kings Authur" }' | python3 -m json.tool
Output:
{ "name": " Kings Authur " }
Advantages of JSON in Python
- Easy to motility back between container and value (JSON to Python and Python to JSON)
- Human readable (Pretty-print) JSON Object
- Widely used in data handling.
- Doesn't take the same data structure in the unmarried file.
Implementation Limitations of JSON in Python
- In deserializer of JSON range and prediction of a number
- The Maximum length of JSON string and arrays of JSON and nesting levels of object.
Python JSON Cheat Sheet
Python JSON Function | Description |
---|---|
json.dumps(person_data) | Create JSON Object |
json.dump(person_data, file_write) | Create JSON File using File I/O of Python |
compact_obj = json.dumps(data, separators=(',',':')) | Meaty JSON Object by removing infinite character from JSON Object using separator |
formatted_obj = json.dumps(dic, indent=iv, separators=(',', ': ')) | Formatting JSON code using Indent |
sorted_string = json.dumps(x, indent=four, sort_keys=True) | Sorting JSON object key by alphabetic society |
complex_obj = json.dumps(four + 5j, default=complex_encode) | Python Complex Object encoding in JSON |
JSONEncoder().encode(colour_dict) | Use of JSONEncoder Class for Serialization |
json.loads(data_string) | Decoding JSON String in Python dictionary using json.loads() function |
json.loads('{"__complex__": true, "real": 4, "img": 5}', object_hook = is_complex) | Decoding of complex JSON object to Python |
JSONDecoder().decode(colour_string) | Use of Decoding JSON to Python with Deserialization |
hosmerpicamortiver1974.blogspot.com
Source: https://www.guru99.com/python-json.html
0 Response to "Json Decode Error When Reading Json Data"
Post a Comment