Skip to content

Instantly share code, notes, and snippets.

@douglaspetrin
Last active October 8, 2019 17:53
Show Gist options
  • Save douglaspetrin/3905d05acbd004fc9e0b1f0904c7e167 to your computer and use it in GitHub Desktop.
Save douglaspetrin/3905d05acbd004fc9e0b1f0904c7e167 to your computer and use it in GitHub Desktop.
Python JSON Encoder & Decoder Overview

JSON Overview

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

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.

Why serialize data?

It can be used to minimize the data's size which can reduces space or badwidth requirements.

How to serialize data?

You can use JSON Python builting library (json.dumps or json.dump).

Compact Enconding

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
    

Usage Example

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}'

Decoding JSON

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)

Usage Example

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}



References:

https://docs.python.org/3/library/json.html#py-to-json-table

https://docs.python-guide.org/scenarios/serialization/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment