0% found this document useful (0 votes)
3 views8 pages

Json 1

JSON (JavaScript Object Notation) is a lightweight, human-readable format for storing and exchanging data, utilizing key-value pairs and arrays. It is widely used in web APIs and app-database communication, and can be easily manipulated in Python using the json module. The document also covers JSON syntax, data types, examples of nested JSON, and how to read and write JSON using Python and pandas.

Uploaded by

sureshsiva0252
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Json 1

JSON (JavaScript Object Notation) is a lightweight, human-readable format for storing and exchanging data, utilizing key-value pairs and arrays. It is widely used in web APIs and app-database communication, and can be easily manipulated in Python using the json module. The document also covers JSON syntax, data types, examples of nested JSON, and how to read and write JSON using Python and pandas.

Uploaded by

sureshsiva0252
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JSON

What is JSON?
JSON (JavaScript Object Notation) is a lightweight, human-readable format used to store and
exchange data.
Key features:

Stores data in key-value pairs (like Python dictionaries)

Can contain lists of objects (arrays)

Text-based and “self-describing”

Widely used in:
○ Web APIs (Browser ↔ Server)
○ App ↔ Database communication
○ Sending data between services
JSON Syntax:

Curly braces {} → Objects / dictionaries

Square brackets [] → Arrays / lists

Data is in name/value pairs, separated by commas
Example JSON object:

{
"name": "John",
"age": 31,
"city": "New York"
}

Python equivalent:

person = {"name": "John", "age": 31, "city": "New York"}

JSON Data Types


JSON Type Example Python Explanation
Equivalent
Object {"key": "value"} dict Key-value pairs
Array [1, 2, 3] list Ordered
collection of
items
String "text" str Text data
Number 123 int / float Numeric data
Boolean true/false True/False Logical values
Null null None Represents
missing value

Example: Nested JSON


JSON can be nested (objects inside arrays or objects).
Example saved as [Link]:

{
"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": null,
"siblings": [
{"name": "Scott", "age": 30, "pets": ["Zeus", "Zuko"]},
{"name": "Katie", "age": 38, "pets": ["Sixes", "Stache", "Cisco"]}
]
}

Python equivalent:

person = {
"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": None,
"siblings": [
{"name": "Scott", "age": 30, "pets": ["Zeus", "Zuko"]},
{"name": "Katie", "age": 38, "pets": ["Sixes", "Stache", "Cisco"]}
]
}

Reading JSON in Python


Using the json module

import json

with open('[Link]') as f:
data = [Link](f) # Load JSON into Python dictionary
print(data)

Output:

{
'name': 'Wes',
'places_lived': ['United States', 'Spain', 'Germany'],
'pet': None,
'siblings': [
{'name': 'Scott', 'age': 30, 'pets': ['Zeus', 'Zuko']},
{'name': 'Katie', 'age': 38, 'pets': ['Sixes', 'Stache', 'Cisco']}
]
}

Reading JSON from a string

import pandas as pd
from io import StringIO
json_str = '[{"id":1,"name":"Ram"},{"id":2,"name":"Sam"}]'
df = pd.read_json(StringIO(json_str))
print(df)

Output:

id name
0 1 Ram
1 2 Sam

Converting Python objects to JSON


Using [Link]

import json

person = {
"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": None,
"siblings": [
{"name": "Scott", "age": 30, "pets": ["Zeus", "Zuko"]},
{"name": "Katie", "age": 38, "pets": ["Sixes", "Stache", "Cisco"]}
]
}

as_json = [Link](person, indent=4) # Convert Python dict to JSON string


print(as_json)

Output (pretty JSON):

{
"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": null,
"siblings": [
{"name": "Scott", "age": 30, "pets": ["Zeus", "Zuko"]},
{"name": "Katie", "age": 38, "pets": ["Sixes", "Stache", "Cisco"]}
]
}

Loads()

loads() function in Python’s json is used to convert a JSON string into a Python object (like a
dictionary or list).

import json

person='''{
"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": null,
"siblings": [
{"name": "Scott", "age": 30, "pets": ["Zeus", "Zuko"]},
{"name": "Katie", "age": 38, "pets": ["Sixes", "Stache", "Cisco"]}
]
}'''

as_json = [Link](person)
print(as_json)

Converting nested JSON to DataFrame


Suppose we want to work with siblings from the earlier JSON:
siblings_df = [Link](as_json['siblings'], columns=['name', 'age'])
print(siblings_df)

Output:

name age
0 Scott 30
1 Katie 38

● Each sibling becomes a row in the DataFrame


● Only selected columns (name and age) are included

Working with JSON in pandas


Reading JSON files

import pandas as pd
df = pd.read_json('[Link]')
print(df)

● [Link] could contain:

[
{"id": 1, "name": "Ram", "marks": 85},
{"id": 2, "name": "Sam", "marks": 90}
]

Output:

id name marks
0 1 Ram 85
1 2 Sam 90

Writing DataFrames to JSON

df.to_json('[Link]', orient='records', indent=4)

Parameters:
●orient='records' → Each row becomes a dictionary → JSON array
●indent=4 → Makes the file readable
Example Output ([Link]):
[
{"id": 1, "name": "Ram", "marks": 85},
{"id": 2, "name": "Sam", "marks": 90}
]

Orient parameter in pandas

Orient Meaning
records List of row dictionaries (common for
APIs)
columns Default → each column is a key,
values are row-indexed dicts
index Use index labels as keys
values Only values (2D array)
split Separates index, columns, and data
Example:

df.to_json('[Link]', orient='columns', indent=4)

Output:

{
"id": {"0":1,"1":2},
"name": {"0":"Ram","1":"Sam"},
"marks": {"0":85,"1":90}
}

Converting nested JSON to DataFrame


Suppose we want to work with siblings from the earlier JSON:

siblings_df = [Link](person['siblings'], columns=['name', 'age'])


print(siblings_df)

Output:

name age
0 Scott 30
1 Katie 38

● Each sibling becomes a row in the DataFrame


● Only selected columns (name and age) are included

Reading JSON strings with different orient


Records format

import pandas as pd
from io import StringIO
json_str = '''
[
{"a":1,"b":2,"c":3},
{"a":4,"b":5,"c":6},
{"a":7,"b":8,"c":9}
]
'''
df = pd.read_json(StringIO(json_str), orient="records")
print(df)

Output:

a b c
0 1 2 3
1 4 5 6
2 7 8 9

Columns format

import pandas as pd
from io import StringIO

json_str = '''
{
"a":{"0":1,"1":4,"2":7},
"b":{"0":2,"1":5,"2":8},
"c":{"0":3,"1":6,"2":9}
}
'''
df = pd.read_json(StringIO(json_str), orient="columns")
print(df)
Output:

a b c
0 1 2 3
1 4 5 6
2 7 8 9

● orient="columns" → Each column is a key, values are dictionaries mapping row index →
value

You might also like