Lesson 23. Working With JSON

JSON is everything that we thought XML was going to be. FIGHT ME!! Back around 2005 when pterodactyls were still delivering the mail, this thing called XML hit the scene. Extensible Markup Language. Sadly, XML failed to deliver on its promises.

Fortunately, along came this thing called JavaScript Object Notation or JSON. JSON is a flexible file format. The key difference between JSON and CSVs is that JSON records can be of any length unlike CSVs where every row has the same number of columns. JSON records can also be nested. All of that means is you can't work with JSON and CSVs the same way without some transformation work. Fortunately, Pandas gives us those tools.

JSON Syntax

{
  "firstName": "Bob",
  "lastName": "Wakefield",
  "isAlive": true,
  "age": 44,
  "address": {
    "streetAddress": "12345 Street",
    "city": "Beverly Hills",
    "state": "CA",
    "postalCode": "90210"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "312.867.5309"
    },
    {
      "type": "office",
      "number": "312.222.2222"
    }
  ],
  "children": [],
  "spouse": null
  "life": "sad"
}

Examples

Example #1: Pulling A JSON File Into A DataFrame

Once you get it in a dataframe, it's smooth sailing from there.

import pandas as pd
df = pd.read_json('https://query.data.world/s/ohb5444qzgzjcm5pdvcyflcsrsedi5')
df

Now you try it!

Don't copy and past. Type the code yourself!

Last updated