Json is a lightweight data interchange format and its module always produces string objects, not bytes objects. That said, you can serialize or deserialize data. But.. What is data serialization?
Data serialization is the process of converting structure data to a format that allows sharing or storage of data in a way that it is possible to recover its original format.
It can be used to minimize the data's size which can reduces space or badwidth requirements.
You can use JSON Python builting library (json.dumps or json.dump).
json.dumps()
Description: Serialize object as a JSON format.
-
json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
-
json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
skipkeys=True --> dict keys that are not of a basic type (string, integer, float, boolean, None) can be skipped instead of raising TypeError sort_keys=True --> the outupt of dicts will be sorted by key
import json
my_object = {
"market_id": "1",
"stock_code": "AZUL4",
"price": "52.25",
"quantity": 100,
"financial_volume": "522500",
"aggressor": "C",
"buying_broker": "3",
"is_after_market": "0",
"selling_broker": "238",
"time": "103244",
"date": "20190919",
"identifier": "009140",
"type": 0
}
my_object_serialized = json.dumps(my_object)
print(my_object_serialized)
>>> '{"market_id": "1", "stock_code": "AZUL4", "price": "52.25", "quantity": 100, "financial_volume": "522500",
"aggressor": "C", "buying_broker": "3", "is_after_market": "0", "selling_broker": "238", "time": "103244",
"date": "20190919", "identifier": "009140", "type": 0}'
json.loads()
Description: Deserialize a string, bytes, bytearray instance containing a JSON document to a Python Object.
-
json.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
-
json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
import json
my_object_serialized = '{"market_id": "1", "stock_code": "AZUL4", "price": "52.25", "quantity": 100,
"financial_volume": "522500", "aggressor": "C", "buying_broker": "3",
"is_after_market": "0", "selling_broker": "238", "time": "103244",
"date": "20190919", "identifier": "009140", "type": 0}'
my_object_deserialized = json.loads(my_object_serialized)
print(my_object_deserialized)
>>> {'market_id': '1',
'stock_code': 'AZUL4',
'price': '52.25',
'quantity': 100,
'financial_volume': '522500',
'aggressor': 'C',
'buying_broker': '3',
'is_after_market': '0',
'selling_broker': '238',
'time': '103244',
'date': '20190919',
'identifier': '009140',
'type': 0}
https://docs.python.org/3/library/json.html#py-to-json-table