Programmer's Python Data - JSON
Written by Mike James   
Wednesday, 03 December 2025
Article Index
Programmer's Python Data - JSON
Multiple Records

JSON is a very popular text data format but it is based on JavaScript can this work with Python?  Find out what lies behind in this extract from Programmer's Python: Everything is Data.

Programmer's Python
Everything is Data

Is now available as a print book: Amazon

pythondata360Contents

  1. Python – A Lightning Tour
  2. The Basic Data Type – Numbers
       Extract: Bignum
  3. Truthy & Falsey
  4. Dates & Times
       Extract Naive Dates
  5. Sequences, Lists & Tuples
       Extract Sequences 
  6. Strings
       Extract Unicode Strings
  7. Regular Expressions
       Extract Simple Regular Expressions 
  8. The Dictionary
       Extract The Dictionary 
  9. Iterables, Sets & Generators
       Extract  Iterables 
  10. Comprehensions
       Extract  Comprehensions 
  11. Data Structures & Collections
       Extract Stacks, Queues and Deques
      
    Extract Named Tuples and Counters
  12. Bits & Bit Manipulation
       Extract Bits and BigNum 
       Extract Bit Masks
  13. Bytes
       Extract Bytes And Strings
       Extract Byte Manipulation 
  14. Binary Files
       Extract Files and Paths 
  15. Text Files
       Extract Text Files & CSV 
       Extract JSON ***NEW!!!
  16. Creating Custom Data Classes
        Extract A Custom Data Class 
  17. Python and Native Code
        Extract   Native Code
    Appendix I Python in Visual Studio Code
    Appendix II C Programming Using Visual Studio Code

<ASIN:1871962765>

<ASIN:1871962749>

<ASIN:1871962595>

<ASIN:B0CK71TQ17>

<ASIN:187196265X>

In chapter but not in this extract

  • Text Files
  • Opening a Text File
  • Text Formats
  • Text Data Formats – CSV
  • The CSV Module
  • CSV Dialects

JSON

JSON, JavaScript Object Notation, is a very attractive way to store data as human readable text. The basic idea is that the syntax for a JavaScript object and the JavaScript array can be used to represent the Python dictionary and the Python list respectively.

In JavaScript an object, which behaves much like a Python dictionary is defined using:

{key1:value1, key2:value2, ...}

A JavaScript array is defined using:

[value1,value1,value2, ...}

Using just these two data structures and a correspondence between primitive data types JSON can represent a wide range of Python data structures. The correspondences between types is:

 

JSON

Python

object

dict

array

list

string

str

number (int)

int

number (real)

float

true

True

false

False

null

None

The similarity is so good that a Python programmer could read JSON as if it was PYON, i.e. PYthon Object Notation. If you regard JavaScript’s object as being a Python dict and its array as a Python list then you only have minor differences to deal with. In particular, the keys in a JSON object are always strings and when a Python dict is converted all of its keys are coerced to strings and when read back they are automatically strings. Keys also cannot be general objects, even if they are hashable.

Notice also that despite the fact that the JSON equivalent of a Python dict is called an object, JSON is not a general way of saving Python objects – it is good for saving arrays and dictionaries in all combinations, but not general objects. Typical uses of JSON are for data exchange and for user-editable configuration files.

The most important methods are dump and load which create and decode JSON respectively. Each is available in two versions – one works with a file and the other works with a string.

To create a JSON coded file or string from a suitable object, obj, use:

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)

and

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)

The parameters that control the conversion are:

  • skipkeys if True will skip dictionary keys that are not str, int, float, bool or None, by default a TypeError exception is raised

  • ensure_ascii if True escapes ASCII characters, otherwise they are output as received

  • check_circular if False then the circular reference check for container types will be skipped and a circular reference will result in a RecursionError

  • allow_nan if False then any non numeric values nan, inf or -inf will throw a ValueError, if True the JavaScript equivalents re used.

  • indent can be a non-negative integer or string that includes indents in the JSON that makes it more human readable

  • separators specifies what white space should be included in separators to make the JSON more human readable

  • default can be set to a function that is called for objects that are not automatically convertible to return a JSON encoding or raise a TypeError

  • sort_keys if True then the output of dictionaries will be sorted by key.

For example, we can easily create a JSON file from a list of mixed data:

import pathlib
import json 
path=pathlib.Path("myTextFile.json")
data=[1,2,{"name":"mike","id":42,"score":3.145}]
with path.open(mode="wt") as f:
    json.dump(data,f)
print(json.dumps(data,indent=4))

 

This writes the JSON to a file and displays it using an indent to make it more readable;

[
    1,
    2,
    {
        "name": "mike",
        "id": 42,
        "score": 3.145
    }
]

To read the data back into Python you can use the corresponding load or loads methods:

json.load(fp, *, cls=None, object_hook=None, 
parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) json.loads(s, *, cls=None, object_hook=None,
parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

where object_hook and object_pairs_hook can be used to define a function to process an object in the JSON. The object_hook function receives the object decoded as a dictionary and the object_pairs_hook receives the object as a list of (key,value) tuples. You can use them to customize how objects are decoded. The three possibilities are:

  • parse_float called with the string of every JSON float

  • parse_int called with the string of every JSON int

  • parse_constant called with '-inf', 'inf', 'NaN'

Using load you can read the data back from the previous example just as easily:

with path.open(mode="rt") as f:
    data= json.load(f)
    print(data)

which displays:

[1, 2, {'name': 'mike', 'id': 42, 'score': 3.145}]

which is what you would expect.



Last Updated ( Wednesday, 03 December 2025 )