JSON - perreaultcourtney/NBIFsummer_projects GitHub Wiki

Introduction

This page provides a basic overview of JSON.
**Note: any specifics (such as functions) are in terms of Python**

IMPORTANT: json package must be imported to Python script.

import json

What is JSON?

  • JavaScript Object Notation
  • Syntax for storing and exchanging data.
  • Web browser to server communication must be done using text.
  • JSON <---> Python Dictionary (conversion between types can occur in both directions).

Syntax

  • JSON Objects are text enclosed in curly braces {}
  • Key/Value pairs: JSON writes text in a way that formats the data into key value pairs.
  • key names must be enclosed using double quotations ""
  • values can be of the following types
Datatype Example
string "example string"
number 65.4
JSON Object {"key" : "internal object"}
array [{"obj1" : "exampVal1"}, {"obj2" : "exampVal2"}]
boolean true / false
null null

Data types and structures

Useful functions/methods

  • loads(<jsonObject>)
  • converts a JSON string to a Python dictionary
  • dumps(<pythonObject>)
  • converts a Python Object to JSON
  • convertible Python Objects:
Python Object JSON Type
dictionary Object
list array
tuple array
string string
int Number
float Number
True true
False false
None null
  • additional arguments for dumps()
  • indent = #
  • sort_keys = #
  • examples

x = {"keyName" : "valueName"} print(json.dumps(x)) output {"keyName" : "valueName"}

References

w3schools:

https://www.w3schools.com/js/js_json_intro.asp https://www.w3schools.com/python/python_json.asp

real python:

https://realpython.com/python-json/

⚠️ **GitHub.com Fallback** ⚠️