Master Methods: Convert JSON to Dictionary using json.load()

How to Convert JSON to Dictionary – Python

Convert json to dictionary using Python,JSON stands for JavaScript Object Notation, which may be a widely used formatting for data interchange on the net. JSON is that the ideal format for organizing data between a client and a server. Its syntax is comparable to the JavaScript artificial language. the most objective of convert json to dictionary for JSON is to transmit the info between the client and also the web server. it’s easy to be told and therefore the handiest thanks to interchange the info. It will be used with various programming languages like Python, Perl, Java, etc.

Sample Json File:

This is the sample file of convert json to dictionary using python json.load() function.

{
    "people1": [
	{
                    "name": "Mike",
                    "website": "mike.com",
                    "from": "Newyork"

                  },
	{
                    "name": "Tyson",
                    "website": "tyson.com",
                    "from": "Canada"
	}
                 ],
    "people2": [
	{
                    "name": "Spark",
                    "website": "spark.com",
                    "from": "America"

                  },
	{
                    "name": "Victoria",
                    "website": "victoria.com",
                    "from": "Australia"
	}
                 ]
}

 

Example #1 Convert JSON to Dictionary:

import json

with open('data.json') as json_file:
    data = json.load(json_file)
    print("Type:", type(data))

    print("\nquiz:", data['quiz'])
    print("\nmaths:", data['maths'])

OUTPUT

Type: <class 'dict'>

People1: [{'name': 'Mike', 'website': 'mike.com', 'from': 'Newyork'}, {'name': 'Tyson', 'website': 'tyson.com', 'from': 'Canada'}]

People2: [{'name': 'Spark', 'website': 'spark.com', 'from': 'America'}, {'name': 'Victoria', 'website': 'victoria.com', 'from': 'Australia'}]

 

Example #2 Convert Json to Dictionary(Reading Nested Data):

Reading only nested for convert json to dictionary using json.load()

import json

with open('data.json') as json_file:
    data = json.load(json_file)
    print(data['people1'][0])
    
    print("\nPrinting nested dictionary as a key-value pair\n")
    for i in data['people1']:
        print("Name:", i['name'])
        print("Website:", i['website'])
        print("From:", i['from'])
        print()

OUTPUT

{'name': 'Mike', 'website': 'mike.com', 'from': 'Newyork'}

Printing nested dictionary as a key-value pair

Name: Mike
Website: mike.com
From: Newyork

Name: Tyson
Website: tyson.com
From: Canada

 

Sample File 2:

{
  "console": "Playstation 5",
  "games": [
    "Spiderman",
    "God of War"
  ]
}

Example #3 Convert JSON to Dictionary:

import json


with open('data.json') as d:
    dictData = json.load(d)
    print(dictData)
    print(type(dictData))
    print(dictData['games'])

OUTPUT

{'console': 'Playstation 5', 'games': ['Spiderman', 'God of War']}
<class 'dict'>
['Spiderman', 'God of War']

 

Example #4 Convert JSON to Dictionary:

import json

videogame = '{"console": "Playstation 5", "games": ["Spiderman", "God of War"]}'
data = json.loads(videogame)

print(data)
print(type(data))

print(data['games'])

OUTPUT

{'console': 'Playstation 5', 'games': ['Spiderman', 'God of War']}
<class 'dict'>
['Spiderman', 'God of War']