„Python“ skaitykite JSON failą - kaip įkelti JSON iš failo ir analizuoti sąvartynus

Sveiki! Jei norite sužinoti, kaip dirbti su JSON failais „Python“, šis straipsnis skirtas jums.

Tu išmoksi:

  • Kodėl JSON formatas yra toks svarbus.
  • Pagrindinė jo struktūra ir duomenų tipai.
  • Kaip „JSON“ ir „Python“ žodynai veikia kartu „Python“.
  • Kaip dirbti su integruotu „Python“   jsonmoduliu.
  • Kaip konvertuoti JSON eilutes į „Python“ objektus ir atvirkščiai.
  • Kaip naudoti loads()irdumps()
  • Kaip automatiškai įtraukti JSON eilutes.
  • Kaip skaityti JSON failus „Python“ naudojant load()
  • Kaip rašyti į JSON failus „Python“ naudojant dump()
  • Ir dar!

Ar tu pasiruošęs? Pradėkime! ✨

? Įvadas: kas yra JSON?

JSON formatą iš pradžių įkvėpė „JavaScript“ (programavimo kalba, naudojama kuriant žiniatinklį) sintaksė. Bet nuo to laiko jis tapo nuo kalbos nepriklausomu duomenų formatu ir dauguma programavimo kalbų, kurias šiandien naudojame, gali generuoti ir skaityti JSON.

JSON svarba ir naudojimo atvejai

JSON iš esmės yra formatas, naudojamas duomenims saugoti ar pateikti. Į jo įprastus naudojimo atvejus įeina žiniatinklio kūrimo ir konfigūracijos failai.

Pažiūrėkime, kodėl:

  • Žiniatinklio plėtra: JSON paprastai naudojamas duomenims siųsti iš serverio klientui ir atvirkščiai žiniatinklio programose.
  • Konfigūracijos failai: JSON taip pat naudojamas konfigūracijoms ir parametrams saugoti. Pvz., Norėdami sukurti „Google Chrome“ programą, turite įtraukti JSON failą, vadinamą manifest.jsonprogramos pavadinimu, jos aprašymu, dabartine versija ir kitomis ypatybėmis bei nustatymais.  

? JSON struktūra ir formatas

Dabar, kai žinote, kam naudojamas JSON formatas, pažiūrėkime jo pagrindinę struktūrą su pavyzdžiu, kuris atspindi picos užsakymo duomenis:

{ "size": "medium", "price": 15.67, "toppings": ["mushrooms", "pepperoni", "basil"], "extra_cheese": false, "delivery": true, "client": { "name": "Jane Doe", "phone": null, "email": "[email protected]" } }

Tai yra pagrindinės JSON formato savybės:

  • Yra raktinių verčių porų seka, supama garbanotais skliaustais {}.
  • Kiekvienas raktas susiejamas su tam tikra verte, naudojant šį formatą:
"key":  

? Patarimas: reikšmės, kurioms reikia kabučių, turi būti apgaubtos kabutėmis.

  • Pagrindinės vertės poros yra atskirtos kableliu. Tik po paskutinės poros kablelis neseka.
{ "size": "medium", # Comma! "price": 15.67 }

? Patarimas: JSON paprastai formatuojame su skirtingais įtraukos lygiais, kad duomenis būtų lengviau skaityti. Šiame straipsnyje sužinosite, kaip automatiškai pridėti įtrauką naudojant „Python“.

JSON duomenų tipai: raktai ir vertės

JSON failai turi specialias taisykles, kurios nustato, kurie duomenų tipai galioja raktams ir reikšmėms.

  • Raktai turi būti styginiai.
  • Vertės gali būti eilutė, skaičius, masyvas, loginė reikšmė ( true/ false) nullarba JSON objektas.

Pagal „Python“ dokumentaciją:

JSON raktų / verčių porų raktai visada yra tokio tipo str. Žodyną pavertus JSON, visi žodyno klavišai priverčiami virtinėmis.

Stiliaus vadovas

Pagal „Google JSON“ stiliaus vadovą:

  • Visada rinkitės prasmingus vardus.
  • Masyvų tipai turėtų turėti daugiskaitos raktų pavadinimus. Visi kiti raktiniai vardai turėtų būti vienaskaitos. Pvz .: naudokite "orders"vietoj, "order"jei atitinkama reikšmė yra masyvas.
  • JSON objektuose neturėtų būti jokių komentarų.

? JSON ir „Python“ žodynai

Iš pradžių „JSON“ ir „Žodynai“ gali atrodyti labai panašūs (vizualiai), tačiau jie yra gana skirtingi. Pažiūrėkime, kaip jie yra „prijungti“ ir kaip jie vienas kitą papildo, kad „Python“ taptų galingu įrankiu dirbti su JSON failais.

JSON yra failo formatas, naudojamas duomenims pateikti ir saugoti, o „Python“ žodynas yra tikroji duomenų struktūra (objektas), kuri laikoma atmintyje, kol veikia „Python“ programa.

Kaip JSON ir „Python“ žodynai veikia kartu

Kai dirbame su JSON failais „Python“, negalime jų tiesiog perskaityti ir tiesiogiai naudoti savo programos duomenis. Taip yra todėl, kad visas failas būtų pavaizduotas kaip viena eilutė ir mes negalėtume pasiekti raktų reikšmių porų atskirai.

Nebent ...

JSON failo raktų ir verčių poras naudojame „Python“ žodynui sukurti, kurį galime naudoti savo programoje duomenims skaityti, naudoti ir modifikuoti (jei reikia).

Tai yra pagrindinis ryšys tarp JSON ir „Python“ žodynų. JSON yra duomenų eilutė, o žodynai yra faktinės atminties duomenų struktūros, sukurtos paleidus programą.

Puiku. Dabar, kai žinote daugiau apie JSON, pradėkime pasinerti į praktinius aspektus, kaip galite dirbti su JSON „Python“.

? JSON modulis

Luckily for us, Python comes with a built-in module called json. It is installed automatically when you install Python and it includes functions to help you work with JSON files and strings.

We will use this module in the coming examples.

How to Import the JSON Module

To use json in our program, we just need to write an import statement at the top of the file.

Like this:

With this line, you will have access to the functions defined in the module. We will use several of them in the examples.

? Tip: If you write this import statement, you will need to use this syntaxto call a function defined in the json module:

? Python and JSON Strings

To illustrate how some of the most important functions of the json module work, we will use a multi-line string with JSON format.

JSON String

Particularly, we will use this string in the examples. It is just a regular multi-line Python string that follows the JSON format.

data_JSON = """ { "size": "Medium", "price": 15.67, "toppings": ["Mushrooms", "Extra Cheese", "Pepperoni", "Basil"], "client": { "name": "Jane Doe", "phone": "455-344-234", "email": "[email protected]" } } """
  • To define a multi-line string in Python, we use triple quotes.  
  • Then, we assign the string to the variable data_JSON.

? Tip: The Python Style Guide recommends using double quote characters for triple-quoted strings.  

JSON String to Python Dictionary

We will use the string with JSON format to create a Python dictionary that we can access, work with, and modify.

To do this, we will use the loads() function of the json module, passing the string as the argument.

This is the basic syntax:

Here is the code:

# Import the module import json # String with JSON format data_JSON = """ { "size": "Medium", "price": 15.67, "toppings": ["Mushrooms", "Extra Cheese", "Pepperoni", "Basil"], "client": { "name": "Jane Doe", "phone": "455-344-234", "email": "[email protected]" } } """ # Convert JSON string to dictionary data_dict = json.loads(data_JSON) 

Let's focus on this line:

data_dict = json.loads(data_JSON)
  • json.loads(data_JSON) creates a new dictionary with the key-value pairs of the JSON string and it returns this new dictionary.
  • Then, the dictionary returned is assigned to the variable data_dict.

Awesome! If we print this dictionary, we see this output:

{'size': 'Medium', 'price': 15.67, 'toppings': ['Mushrooms', 'Extra Cheese', 'Pepperoni', 'Basil'], 'client': {'name': 'Jane Doe', 'phone': '455-344-234', 'email': '[email protected]'}}

The dictionary has been populated with the data of the JSON string. Each key-value pair was added successfully.

Now let's see what happens when we try to access the values of the key-value pairs with the same syntax that we would use to access the values of a regular Python dictionary:

print(data_dict["size"]) print(data_dict["price"]) print(data_dict["toppings"]) print(data_dict["client"])

The output is:

Medium 15.67 ['Mushrooms', 'Extra Cheese', 'Pepperoni', 'Basil'] {'name': 'Jane Doe', 'phone': '455-344-234', 'email': '[email protected]'}

Exactly what we expected. Each key can be used to access its corresponding value.

? Tip: We can use this dictionary just like any other Python dictionary. For example, we can call dictionary methods, add, update, and remove key-value pairs, and more. We can even use it in a for loop.

JSON to Python: Type Conversion

When you use loads() to create a Python dictionary from a JSON string, you will notice that some values will be converted into their corresponding Python values and data types.

This table presented in the Python Documentation for the json module summarizes the correspondence from JSON data types and values to Python data types and values:

? Tip: The same conversion table applies when we work with JSON files.

Python Dictionary to JSON String

Now you know how to create a Python dictionary from a string with JSON format.

But sometimes we might need to do exactly the opposite, creating a string with JSON format from an object (for example, a dictionary) to print it, display it, store it, or work with it as a string.

To do that, we can use the dumps function of the json module, passing the object as argument:

? Tip: This function will return a string.

This is an example where we convert the Python dictionary client into a string with JSON format and store it in a variable:

# Python Dictionary client = { "name": "Nora", "age": 56, "id": "45355", "eye_color": "green", "wears_glasses": False } # Get a JSON formatted string client_JSON = json.dumps(client)

Let's focus on this line:

client_JSON = json.dumps(client)
  • json.dumps(client) creates and returns a string with all the key-value pairs of the dictionary in JSON format.
  • Then, this string is assigned to the client_JSON variable.

If we print this string, we see this output:

{"name": "Nora", "age": 56, "id": "45355", "eye_color": "green", "wears_glasses": false}

? Tip: Notice that the last value (false) was changed. In the Python dictionary, this value was False but in JSON, the equivalent value is false. This helps us confirm that, indeed, the original dictionary is now represented as a string with JSON format.

If we check the data type of this variable, we see:

So the return value of this function was definitely a string.

Python to JSON: Type Conversion

A process of type conversion occurs as well when we convert a dictionary into a JSON string. This table from the Python Documentation illustrates the corresponding values:

How to Print JSON With Indentation

If we use the dumps function and we print the string that we got in the previous example, we see:

{"name": "Nora", "age": 56, "id": "45355", "eye_color": "green", "wears_glasses": false}

But this is not very readable, right?

We can improve the readability of the JSON string by adding indentation.

To do this automatically, we just need to pass a second argument to specify the number of spaces that we want to use to indent the JSON string:

? Tip: the second argument has to be a non-negative integer (number of spaces) or a string. If indent is a string (such as "\t"), that string is used to indent each level (source).

Now, if we call dumps with this second argument:

client_JSON = json.dumps(client, indent=4)

The result of printing client_JSON is:

{ "name": "Nora", "age": 56, "id": "45355", "eye_color": "green", "wears_glasses": false }

That's great, right? Now our string is nicely formatted. This will be very helpful when we start working with files to store the data in a human-readable format.

How to Sort the Keys

You can also sort the keys in alphabetical order if you need to. To do this, you just need to write the name of the parameter sort_keys and pass the value True:

? Tip: The value of sort_keys is False by default if you don't pass a value.

For example:

client_JSON = json.dumps(client, sort_keys=True)

Returns this string with the keys sorted in alphabetical order:

{"age": 56, "eye_color": "green", "id": "45355", "name": "Nora", "wears_glasses": false}

How to Sort Alphabetically and Indent (at the same time)

To generate a JSON string that is sorted alphabetically and indented, you just need to pass the two arguments:

In this case, the output is:

{ "age": 56, "eye_color": "green", "id": "45355", "name": "Nora", "wears_glasses": false }

? Tip: You can pass these arguments in any order (relative to each other), but the object has to be the first argument in the list.

Great. Now you know how to work with JSON strings, so let's see how you can work with JSON files in your Python programs.

? JSON and Files

Typically, JSON is used to store data in files, so Python gives us the tools we need to read these types of file in our program, work with their data, and write new data.

? Tip: a JSON file has a .json extension:

Let's see how we can work with .json files in Python.

How to Read a JSON File in Python

Let's say that we created an orders.json file with this data that represents two orders in a pizza shop:

{ "orders": [ { "size": "medium", "price": 15.67, "toppings": ["mushrooms", "pepperoni", "basil"], "extra_cheese": false, "delivery": true, "client": { "name": "Jane Doe", "phone": null, "email": "[email protected]" } }, { "size": "small", "price": 6.54, "toppings": null, "extra_cheese": true, "delivery": false, "client": { "name": "Foo Jones", "phone": "556-342-452", "email": null } } ] }

Please take a moment to analyze the structure of this JSON file.

Here are some quick tips:

  • Notice the data types of the values, the indentation, and the overall structure of the file.
  • The value of the main key "orders" is an array of JSON objects (this array will be represented as list in Python). Each JSON object holds the data of a pizza order.

If we want to read this file in Python, we just need to use a with statement:

? Tip: In the syntax above, we can assign any name to file (green box). This is a variable that we can use within the with statement to refer to the file object.

The key line of code in this syntax is:

data = json.load(file)
  • json.load(file) creates and returns a new Python dictionary with the key-value pairs in the JSON file.
  • Then, this dictionary is assigned to the data variable.

? Tip: Notice that we are using load() instead of loads(). This is a different function in the json module. You will learn more about their differences at the end of this article.

Once we have the content of the JSON file stored in the data variable as a dictionary, we can use it to do basically anything we want.

Examples

For example, if we write:

print(len(data["orders"]))

The output is 2 because the value of the main key "orders" is a list with two elements.

We can also use the keys to access their corresponding values. This is what we typically do when we work with JSON files.

For example, to access the toppings of the first order, we would write:

data["orders"][0]["toppings"]
  • First, we select the main key "orders"
  • Then, we select the first element in the list (index 0).
  • Finally, we select the value that corresponds to the key "toppings"

You can see this "path" graphically in the diagram:

If we print this value, the output is:

['mushrooms', 'pepperoni', 'basil']

Exactly what we expected. You just need to "dive deeper" into the structure of the dictionary by using the necessary keys and indices. You can use the original JSON file/string as a visual reference. This way, you can access, modify, or delete any value.

? Tip: Remember that we are working with the new dictionary. The changes made to this dictionary will not affect the JSON file. To update the content of the file, we need to write to the file.

How to Write to a JSON File

Let's see how you can write to a JSON file.

The first line of the with statement is very similar. The only change is that you need to open the file in 'w' (write) mode to be able to modify the file.

? Tip: If the file doesn't exist already in the current working directory (folder), it will be created automatically. By using the 'w' mode, we will be replacing the entire content of the file if it already exists.

There are two alternative ways to write to a JSON file in the body of the with statement:

  • dump
  • dumps

Let's see them in detail.

First Approach: dump

This is a function that takes two arguments:

  • The object that will be stored in JSON format (for example, a dictionary).
  • The file where it will be stored (a file object).

Let's say that the pizza shop wants to remove the clients' data from the JSON file and create a new JSON file called orders_new.json with this new version.

We can do this with this code:

# Open the orders.json file with open("orders.json") as file: # Load its content and make a new dictionary data = json.load(file) # Delete the "client" key-value pair from each order for order in data["orders"]: del order["client"] # Open (or create) an orders_new.json file # and store the new version of the data. with open("orders_new.json", 'w') as file: json.dump(data, file)

This was the original version of the data in the orders.json file. Notice that the "client" key-value pair exists.

{ "orders": [ { "size": "medium", "price": 15.67, "toppings": ["mushrooms", "pepperoni", "basil"], "extra_cheese": false, "delivery": true, "client": { "name": "Jane Doe", "phone": null, "email": "[email protected]" } }, { "size": "small", "price": 6.54, "toppings": null, "extra_cheese": true, "delivery": false, "client": { "name": "Foo Jones", "phone": "556-342-452", "email": null } } ] } 

This is the new version in the orders_new.json file:

{"orders": [{"size": "medium", "price": 15.67, "toppings": ["mushrooms", "pepperoni", "basil"], "extra_cheese": false, "delivery": true}, {"size": "small", "price": 6.54, "toppings": null, "extra_cheese": true, "delivery": false}]}

If you analyze this carefully, you will see that the "clients" key-value pair was removed from all the orders.

However, there is something missing in this file, right?

Please take a moment to think about this... What could it be?

Indentation, of course!

The file doesn't really look like a JSON file, but we can easily fix this by passing the argument indentation=4 to dump().

Now the content of the file looks like this:

{ "orders": [ { "size": "medium", "price": 15.67, "toppings": [ "mushrooms", "pepperoni", "basil" ], "extra_cheese": false, "delivery": true }, { "size": "small", "price": 6.54, "toppings": null, "extra_cheese": true, "delivery": false } ] }

What a difference! This is exactly what we would expect a JSON file to look like.

Now you know how to read and write to JSON files using load() and dump(). Let's see the differences between these functions and the functions that we used to work with JSON strings.  

? load() vs. loads()

This table summarizes the key differences between these two functions:

? Tip: Think of loads() as "load string" and that will help you remember which function is used for which purpose.

? dump() vs. dumps()

Here we have a table that summarizes the key differences between these two functions:

? Tip: Think of dumps() as a "dump string" and that will help you remember which function is used for which purpose.

? Important Terminology in JSON

Finally, there are two important terms that you need to know to work with JSON:

  • Serializavimas: objekto pavertimas JSON eilute.
  • Dezerializavimas: JSON eilutės pavertimas objektu.

? Apibendrinant

  • JSON („JavaScript Object Notation“) yra formatas, naudojamas duomenims pateikti ir saugoti.
  • Paprastai jis naudojamas duomenims perduoti internete ir konfigūracijos parametrams išsaugoti.
  • JSON failai turi .jsonplėtinį.
  • Galite konvertuoti JSON eilutes į „Python“ objektus ir atvirkščiai.
  • Galite skaityti JSON failus ir kurti „Python“ objektus iš jų raktų ir reikšmių porų.
  • Galite įrašyti į JSON failus, kad išsaugotumėte „Python“ objektų turinį JSON formatu.

Labai tikiuosi, kad jums patiko mano straipsnis ir jis buvo naudingas. Dabar jūs žinote, kaip dirbti su JSON „Python“. Sekite mane „Twitter“ @EstefaniaCassN ir peržiūrėkite mano internetinius kursus.