Tutorials
Hands-On Python
Hands-On Python
  • Hands-On Python Tutorial For Real-World Business Analytics Problems
  • Preface
    • Section I. A Note From The Author
    • Section II. Tutorial Overview
    • Section III. What Is The Preflight Checklist?
    • Section IV. Supplimentery Material
  • Preflight Checklist
    • Section V. Select Your Difficulty Setting
    • Section VI. Download Anaconda
    • Section VII. Download PyCharm (Optional)
    • Section VIII. Download SQL Server Developer Edition
    • Section IX. Configure Database Environment
    • Section X. Download The Source Code
    • Section XI. Starting JupyterLab
    • Section XII. How To Get Help With This Tutorial
  • Language Basics
    • Lesson 1. Obligatory Hello World
    • Lesson 2. Code Comments
    • Lesson 3. Data Types
    • Lesson 4. Variables
    • Lesson 5. String Concatenation
    • Lesson 6. Arithmetic Operators
    • Lesson 7. Making Decisions
    • Lesson 8. Control Flow With if-elif-else
    • Lesson 9. Control Flow With while
    • Lesson 10. Data Structures Part I: List
    • Lesson 11. Data Structures Part II: Tuples
    • Lesson 12. Data Structures Part III: Dictionaries
    • Lesson 13. Looping With for
    • Lesson 14. Functions
    • Lesson 15. Importing Modules
    • Lesson 16. Python Programming Standards
  • Advanced Topics
    • Lesson 17. Functional Programing With map
    • Lesson 18. Generators
    • Lesson 19. Comprehensions
    • Lesson 20. Basic File Operations
    • Lesson 21. Working With Data In Numpy
    • Lesson 22. Working With Data In Pandas
    • Lesson 23. Working With JSON
    • Lesson 24. Making File Request Over HTTP And SFTP
    • Lesson 25. Interacting With Databases
    • Lesson 26. Saving Objects With Pickle
    • Lesson 27. Error Handling
    • Lesson 28. Bringing It All Together
  • Solutions To Real World Problems
    • Lesson 29. Download A Zip File Over HTTP
    • Lesson 30. Looping Over Files In A Directory
    • Lesson 31. Convert Comma Delmited Files To Pipe Delimited
    • Lesson 32. Combining Multiple CSVs Into One File
    • Lesson 33. Load Large CSVs Into Data Warehouse Staging Tables
    • Lesson 34. Efficiently Write Large Database Query Results To Disk
    • Lesson 35. Working With SFTP In The Real World
    • Lesson 36. Executing Python From SQL Server Agent
Powered by GitBook
On this page
  • JSON Syntax
  • Examples
  • Now you try it!
  1. Advanced Topics

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!

PreviousLesson 22. Working With Data In PandasNextLesson 24. Making File Request Over HTTP And SFTP

Last updated 3 years ago