Python read json files

Work with JSON files#

JSON (JavaScript Object Notation) — a text format for data storage and exchange.

JSON syntax is very similar to Python and is user-friendly.

As for CSV, Python has a module that allows easy writing and reading of data in JSON format.

Reading#

 "access": [ "switchport mode access", "switchport access vlan", "switchport nonegotiate", "spanning-tree portfast", "spanning-tree bpduguard enable" ], "trunk": [ "switchport trunk encapsulation dot1q", "switchport mode trunk", "switchport trunk native vlan 999", "switchport trunk allowed vlan" ] > 

There are two methods for reading in json module:

  • json.load — method reads JSON file and returns Python objects
  • json.loads — method reads string in JSON format and returns Python objects

json.load #

Reading JSON file to Python object (json_read_load.py file):

import json with open('sw_templates.json') as f: templates = json.load(f) print(templates) for section, commands in templates.items(): print(section) print('\n'.join(commands)) 

The output will be as follows:

$ python json_read_load.py access switchport mode access switchport access vlan switchport nonegotiate spanning-tree portfast spanning-tree bpduguard enable trunk switchport trunk encapsulation dot1q switchport mode trunk switchport trunk native vlan 999 switchport trunk allowed vlan

json.loads #

Reading JSON string to Python object (json_read_loads.py file):

import json with open('sw_templates.json') as f: file_content = f.read() templates = json.loads(file_content) print(templates) for section, commands in templates.items(): print(section) print('\n'.join(commands)) 

The result will be similar to previous output.

Writing#

Writing a file in JSON format is also fairly easy.

There are also two methods for writing information in JSON format in json module:

  • json.dump — method writes Python object to file in JSON format
  • json.dumps — method returns string in JSON format

json.dumps()#

Convert object to string in JSON format (json_write_dumps.py):

import json trunk_template = [ 'switchport trunk encapsulation dot1q', 'switchport mode trunk', 'switchport trunk native vlan 999', 'switchport trunk allowed vlan' ] access_template = [ 'switchport mode access', 'switchport access vlan', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable' ] to_json = 'trunk': trunk_template, 'access': access_template> with open('sw_templates.json', 'w') as f: f.write(json.dumps(to_json)) with open('sw_templates.json') as f: print(f.read()) 

Method json.dumps is suitable for situations where you want to return a string in JSON format. For example, to pass it to the API.

json.dump #

Write a Python object to a JSON file (json_write_dump.py file):

import json trunk_template = [ 'switchport trunk encapsulation dot1q', 'switchport mode trunk', 'switchport trunk native vlan 999', 'switchport trunk allowed vlan' ] access_template = [ 'switchport mode access', 'switchport access vlan', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable' ] to_json = 'trunk': trunk_template, 'access': access_template> with open('sw_templates.json', 'w') as f: json.dump(to_json, f) with open('sw_templates.json') as f: print(f.read()) 

When you want to write information in JSON format into a file, it is better to use dump method.

Additional parameters of write methods#

Methods dump and dumps can pass additional parameters to manage the output format.

By default, these methods write information in a compact view. As a rule, when data is used by other programs, visual presentation of data is not important. If data in file needs to be read by person, this format is not very convenient to perceive. Fortunately, json module allows you to manage such things.

By passing additional parameters to dump method (or dumps method) you can get a more readable output (json_write_indent.py file):

import json trunk_template = [ 'switchport trunk encapsulation dot1q', 'switchport mode trunk', 'switchport trunk native vlan 999', 'switchport trunk allowed vlan' ] access_template = [ 'switchport mode access', 'switchport access vlan', 'switchport nonegotiate', 'spanning-tree portfast', 'spanning-tree bpduguard enable' ] to_json = 'trunk': trunk_template, 'access': access_template> with open('sw_templates.json', 'w') as f: json.dump(to_json, f, sort_keys=True, indent=2) with open('sw_templates.json') as f: print(f.read()) 

Now the content of sw_templates.json file is:

 "access": [ "switchport mode access", "switchport access vlan", "switchport nonegotiate", "spanning-tree portfast", "spanning-tree bpduguard enable" ], "trunk": [ "switchport trunk encapsulation dot1q", "switchport mode trunk", "switchport trunk native vlan 999", "switchport trunk allowed vlan" ] > 

Changing data type#

Another important aspect of data conversion to JSON format is that data will not always be the same type as source data in Python.

For example, when you write a tuple to JSON it becomes a list:

In [1]: import json In [2]: trunk_template = ('switchport trunk encapsulation dot1q', . 'switchport mode trunk', . 'switchport trunk native vlan 999', . 'switchport trunk allowed vlan') In [3]: print(type(trunk_template)) In [4]: with open('trunk_template.json', 'w') as f: . json.dump(trunk_template, f, sort_keys=True, indent=2) . In [5]: cat trunk_template.json [ "switchport trunk encapsulation dot1q", "switchport mode trunk", "switchport trunk native vlan 999", "switchport trunk allowed vlan" ] In [6]: templates = json.load(open('trunk_template.json')) In [7]: type(templates) Out[7]: list In [8]: print(templates) ['switchport trunk encapsulation dot1q', 'switchport mode trunk', 'switchport trunk native vlan 999', 'switchport trunk allowed vlan']

This is because JSON uses different data types and does not have matches for all Python data types.

Python data conversion table to JSON:

Источник

How do I read a json file into python?

I’m new to JSON and Python, any help on this would be greatly appreciated. I read about json.loads but am confused How do I read a file into Python using json.loads? Below is my JSON file format:

My requirement is to read the values of all «abc» «def» in details and add this is to a new list like this [(1,2),(3,4),(5,6),(7,8)] . The new list will be used to create a spark data frame.

2 Answers 2

Open the file, and get a filehandle:

Then, pass the file handle into json.load(): (don’t use loads — that’s for strings)

import json data = json.load(fh) 

From there, you can easily deal with a python dictionary that represents your json-encoded data.

new_list = [(detail['abc'], detail['def']) for detail in data['details']] 

Note that your JSON format is also wrong. You will need comma delimiters in many places, but that’s not the question.

I’m trying to understand your question as best as I can, but it looks like it was formatted poorly.

First off your json blob is not valid json, it is missing quite a few commas. This is probably what you are looking for:

Now assuming you are trying to parse this in python you will have to do the following.

import json json_blob = ',"details": [,,]>' json_obj = json.loads(json_blob) final_list = [] for single in json_obj['details']: final_list.append((int(single['abc']), int(single['def']))) print(final_list) 

This will print the following: [(3, 4), (5, 6), (7, 8)]

Источник

Loading a JSON File in Python – How to Read and Parse JSON

Dillion Megida

Dillion Megida

Loading a JSON File in Python – How to Read and Parse JSON

In this article, you’ll learn how to read and parse JSON in Python.

What is JSON?

JSON is short for JavaScript Object Notation. It’s a simple syntax for storing data in name-value pairs. Values can be different data types as long as they are valid. Non-acceptable types for JSON include functions, dates, and undefined .

JSON files are stored with the .json extension with a valid JSON structure.

Here’s what the structure of a JSON file looks like:

You’ll often use JSON to send and receive data from a server in web applications.

When the data is received, the program reads and parses the JSON to extract specific data. Different languages have their own methods for doing this. We’ll look at how to do these in Python here.

How to Read JSON Files

Let’s say the JSON in the code block above is stored in a user.json file. Using the open() inbuilt function in Python, we can read that file and assign the content to a variable. Here’s how:

with open('user.json') as user_file: file_contents = user_file.read() print(file_contents) #

You pass the file path to the open method which opens the file and assigns the stream data from the file to the user_file variable. Using the read method, you can pass the text contents of the file to the file_contents variable.

I used with at the beginning of the expression so that after reading the contents of the file, Python can close the file.

file_contents now contains a stringified version of the JSON. As a next step, you can now parse the JSON.

How to Parse JSON

Python has in-built modules for various operations. For managing JSON files, Python has the json module.

This module comes with many methods. One of which is the loads() method for parsing JSON strings. Then, you can assign the parsed data to a variable like this:

import json with open('user.json') as user_file: file_contents = user_file.read() print(file_contents) parsed_json = json.loads(file_contents) #

Using the loads() method, you can see that the parsed_json variable now has a valid dictionary. From this dictionary, you can access the keys and values in it.

Also notice how null from the JSON is converted to None in python. This is because null is not valid in Python .

How to Use json.load() to Read and Parse JSON Files

The json module also has the load method which you can use to read a file object and parse it at the same time. Using this method, you can update the previous code to this:

import json with open('user.json') as user_file: parsed_json = json.load(user_file) print(parsed_json) #

Instead of using the read method of the file object and using the loads method of the json module, you can directly use the load method which reads and parses the file object.

Wrapping Up

JSON data is commonly known for its simple structure and is popular (a standard in most cases) for information exchange between servers and clients.

Different languages and technologies can read and parse JSON files in different ways. In this article, we’ve learned how to read JSON files and parse such files using the read method of file objects, and the loads and load methods of the json module.

Dillion Megida

Dillion Megida

Developer Advocate and Content Creator passionate about sharing my knowledge on Tech. I simplify JavaScript / ReactJS / NodeJS / Frameworks / TypeScript / et al My YT channel: youtube.com/c/deeecode

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

Читайте также:  Https elearning otr ru mod book view php id 1417 chapterid 2228
Оцените статью