JSON (JavaScript Object Notation) – Python Document is a lightweight data interchange format that provides a structured representation of data. In Python, you can easily manipulate JSON data using the built-in json module. In this article, I’ll explain how to encode (serialize) and decode (deserialize) JSON in Python.

JSON is a lightweight data interchange format defined in RFC 7159 (replacing the previous RFC 4627) and ECMA-404 that is inspired by the JavaScript object syntax, but is not a subset of JavaScript.

Features of JSON

  • Lightweight and simple: More compact than XML, reducing data transfer size.
  • Highly readable: Easy for humans to read and understand.
  • Language-independent: Works with many languages, including Python, Java, C++, and Go, not just JavaScript.
  • Key-value pair structure: Makes data representation intuitive.
  • Supports arrays and nested structures: Can handle complex data structures efficiently.

json.dumps() Parameters

  • indent : 들여쓰기 적용 (json.dumps(data, indent=4))
  • sort_keys : 키 정렬 (json.dumps(data, sort_keys=True))
  • ensure_ascii : ASCII 문자 강제 (json.dumps(data, ensure_ascii=False))

json.loads() Features

  • Converting JSON strings to Python’s native data types (dictionaries, lists, etc.)
  • Possible TypeError, ValueError exceptions (when entering an invalid JSON format)

Json Python Code

import json

#References : https://www.alphavantage.co/
source_string = 
    '{"Meta Data": {"economy": "Intraday (5min) open, high, low, ' \
    'close prices and volume", "Symbol": "IBM", "Last Refreshed": ' \
    '"2024-07-12 19:50:00", "Interval": "5min", "Output Size": ' \
    '"Compact", "6. Time Zone": "US/Eastern"}, "Time Series (5min)": ' \
    '{"2024-07-12 19:50:00": {"open": "182.7850", "high": ' \
    '"182.7850", "low": "182.7000", "close": "182.7050", "volume": ' \
    '"40"}, "2024-07-12 19:40:00": {"open": "182.7700", "high": ' \
    '"182.8300", "low": "182.7700", "close": "182.8300", "volume": ' \
    '"527"}, "2024-07-12 19:35:00": {"open": "182.7000", "high": ' \
    '"182.7000", "low": "182.7000", "close": "182.7000", "' \
    'volume": "111"}}}'

# decoder
json_data = json.loads(source_string)

print(json_data["Meta Data"]["economy"])

for data in json_data["Time Series (5min)"]:
    _data = json_data["Time Series (5min)"][data]
    avg = (float(tmp_data["high"]) + float(tmp_data["low"]))/2
    print(data, "open:", _data["open"], "AVG:", avg, "volume:", _data["volume"] )

# encoder
print(type(json_data)) # <class 'dict'>
print(type(json.dumps(json_data))) # <class 'str'>

Output

Intraday (5min) open, high, low, close prices and volume
2024-07-12 19:50:00 open: 182.7850 AVG: 182.7425 volume: 40
2024-07-12 19:40:00 open: 182.7700 AVG: 182.8 volume: 527
2024-07-12 19:35:00 open: 182.7000 AVG: 182.7 volume: 111
<class 'dict'>
<class 'str'>

Troubleshooting possible issues with JSON conversion

  1. Unicode character issues: Use ensure_ascii=False to preserve UTF-8 encoding when working with non-ASCII characters.
  2. Datetime serialization issues: Use default=str or isoformat() to properly format datetime objects.
  3. Object conversion issues: Use the default parameter to define custom conversion functions for non-serializable objects.

JSON vs. other data formats

FormattingProsDisadvantages
JSONLightweight and highly readable, supported by most languagesLimited data types, less expressive than XML
XMLCan represent structured data, good for documentation-type dataLess readable and large
YAMLHuman readable, supports annotationsCan be slow to parse

Python’s json module simplifies encoding and decoding JSON data.

Beyond the basic dumps() and loads() functions, you can:

  • Save and load JSON files using dump() and load().
  • Convert custom objects using the default parameter in dumps().

By leveraging these features, you can efficiently process and manage JSON data in your applications.

By Mark

-_-

Leave a Reply

Your email address will not be published. Required fields are marked *