- Open Standard widely used in Web APIs today.
- Purpose mainly for data serialization.
- Indentation is completely optional, except for with string literals.
- MAIN DATA STRUCTURES:
- Objects/Dictionary
- Groups of key value pairs.
- Key must be a string.
- Value can be objects, numbers, lists, boolean, strings or Null.
- Lists
- Ordered sequence of elements.
- Elements can be same type as object values.
- Can be objects, numbers, lists, boolean, strings or Null.
- Objects/Dictionary
- Object/Dictionary ex.
- Curly braces and double quotes encapsulate name:value pairs, separated by commas.
- {“R10″:”4431”, “R20:”2921”, “R30″:3850”}
- Curly braces and double quotes encapsulate name:value pairs, separated by commas.
- List ex.
- Same as Python
- Encapsulated with square brackets and double quotes, separated by commas.
- [“R1”, “R2”, “R3”]
- Multi-Line Notation – common, ultimately doesn’t matter because whitespaces do not matter.
- Using JSON in Python
import json
with open(“example.json”) as f:
data = f.read()
jsonDict = json.loads(data)
for k, v in jsonDict.items():
print( “The key {} uses a {} value.”.format(str(k), str(type(v))) )
- with open(“example.json”) as f:
- With open for better exception handling and closes file when done.
- json.loads(data)
- What converts imported string (with open <file>) into JSON object/dictionary.