Sunday, June 21, 2020

JSON

JSON

 There's a JSON module that can be imported.


 JSON is a protocol that is used for storing data or exchanging data described as JavaScript objects.

 JSON can be read into python and the resulting variable will be a dictionary. Use the loads() method to bring in JSON objects and use the dumps() method if you want to go from python to JSON.


import json

# example JSON:
x = '{ "team":"Lions", "size":20, "field":"grass"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y)

# note that JSON to Python drops the double quote marks in favor of # single quote marks, and the outermost pair of single quotes
# enclosing the entire JavaScript object is dropped by python.

{'team': 'Lions', 'size': 20, 'field': 'grass'}


import json

# a Python object (dict):
x = {
  "team": "Lions",
  "size": 20,
  "field": "grass"
}

# convert into JSON:
y = json.dumps(x)

# the result is a JSON string. Notice the curly braces and the reverse ordering.

print(y)

{"team": "Lions", "size": 20, "field": "grass"}


Converting a python tuple goes into a JSON with square braces.
 
Converting the python None value goes into the JSON null value.

Converting a python pair of single quotes or double quotes goes to a JSON pair of double quotes.

Converting python scientific notation into JSON with all the scientific notation expanded and with one numeral to the right of the decimal point.

Optional format parameters for the dumps() method:

You say indent = number of spaces when you want smart indenting automatically done.

separators = in a pair of parentheses and double quotes; the first one tells you how to separate the objects and the second pair of double quotes tells you how to separate the key-value pairs.

The other option is order which is a Boolean (True or False) if you want them alphabetically ordered.

Here is an example that demonstrates all the possible conversions and formatting options.

# convert python to JSON

import json

# the variable business is a python dictionary
# founders variable is a tuple
# floors variable is a list
# which itself contains two short dictionaries

business = {
  "location": "John",
  "years in business": 30,
  "open at night": True,
  "profitable": False,
  "founders": ("Jackson","Emily"), 
  "parking lot": None,
  "floors": [
    {"first": "legal", "lawyers": 15},
    {"second": "collections", "dollars": 24000.46}
  ]
}

# use - and a space to separate objects, and 
# a space and a = to separate keys from their values:

print(json.dumps(business, indent=4, separators=("- ", "= ")))

###########   JSON output ##############

{
    "location"= "John"- 
    "years in business"= 30- 
    "open at night"= true- 
    "profitable"= false- 
    "founders"= [
        "Jackson"- 
        "Emily"
    ]- 
    "parking lot"= null- 
    "floors"= [
        {
            "first"= "legal"- 
            "lawyers"= 15
        }- 
        {
            "second"= "collections"- 
            "dollars"= 24000.46
        }
    ]
}


No comments:

Post a Comment