Reading and writing JSON files
JavaScript Object Notation (JSON) is an open source file format used to store and transport data. It can easily be parsed into a JavaScript object. JSON is language independent and is used in projects with other programming languages that require a lightweight data exchange format. JSON stores and represents data as key-value pairs. In Python terms, JSON is very much like data that is stored in Python dictionaries.
In this recipe, we’ll cover how to read and write JSON files in Polars. We’ll also cover how to work with a different variation of JSON: Newline Delimited JSON (NDJSON). It is also called JSON Lines (JSONL) or Line-Delimited JSON (LDJSON). As the name suggests, each line is a JSON object.
How to do it...
Next, we’ll dive into how to work with JSON files in Polars:
- Read a JSON file, showing the first 10 columns:
df = pl.read_json('../data/world_population.json') df.select(df.columns[:10]).head(...