0% found this document useful (0 votes)
17 views

Handling JSON

Uploaded by

Pranay Reddy
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)
17 views

Handling JSON

Uploaded by

Pranay Reddy
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
You are on page 1/ 4

How to Handle JSON File

import pandas as pd

x = '{"Name":["p1","p2"],"Age":[21,23]}'
## default json format simplest json

pd.read_json(x)

C:\Users\phane\AppData\Local\Temp\ipykernel_21116\1267235832.py:1: FutureWarning: Passing literal json to 'read_


json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'String
IO' object.
pd.read_json(x)
Name Age

0 p1 21

1 p2 23

x = '{1:["p1","p2"],"Age":[21,23]}' ## keys only take string datatype


## Can't use int,float,complex etc only strings

pd.read_json(x)

C:\Users\phane\AppData\Local\Temp\ipykernel_21116\1267235832.py:1: FutureWarning: Passing literal json to 'read_


json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'String
IO' object.
pd.read_json(x)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[13], line 1
----> 1 pd.read_json(x)

File ~\AppData\Roaming\Python\Python311\site-packages\pandas\io\json\_json.py:804, in read_json(path_or_buf, ori


ent, typ, dtype, convert_axes, convert_dates, keep_default_dates, precise_float, date_unit, encoding, encoding_e
rrors, lines, chunksize, compression, nrows, storage_options, dtype_backend, engine)
802 return json_reader
803 else:
--> 804 return json_reader.read()

File ~\AppData\Roaming\Python\Python311\site-packages\pandas\io\json\_json.py:1014, in JsonReader.read(self)


1012 obj = self._get_object_parser(self._combine_lines(data_lines))
1013 else:
-> 1014 obj = self._get_object_parser(self.data)
1015 if self.dtype_backend is not lib.no_default:
1016 return obj.convert_dtypes(
1017 infer_objects=False, dtype_backend=self.dtype_backend
1018 )

File ~\AppData\Roaming\Python\Python311\site-packages\pandas\io\json\_json.py:1040, in JsonReader._get_object_pa


rser(self, json)
1038 obj = None
1039 if typ == "frame":
-> 1040 obj = FrameParser(json, **kwargs).parse()
1042 if typ == "series" or obj is None:
1043 if not isinstance(dtype, bool):

File ~\AppData\Roaming\Python\Python311\site-packages\pandas\io\json\_json.py:1173, in Parser.parse(self)


1172 def parse(self):
-> 1173 self._parse()
1175 if self.obj is None:
1176 return None

File ~\AppData\Roaming\Python\Python311\site-packages\pandas\io\json\_json.py:1366, in FrameParser._parse(self)


1362 orient = self.orient
1364 if orient == "columns":
1365 self.obj = DataFrame(
-> 1366 ujson_loads(json, precise_float=self.precise_float), dtype=None
1367 )
1368 elif orient == "split":
1369 decoded = {
1370 str(k): v
1371 for k, v in ujson_loads(json, precise_float=self.precise_float).items()
1372 }

ValueError: Key name of object must be 'string' when decoding 'object'

simplest form of json is


where keys -- acting as column names

where values --- acting as column values

x = '{"Name":["p1","p2"],"Age":[21,23]}'

Different formats of json

x = '{"Name":["p1","p2"],"Age":[21,23]}'

data=pd.read_json(x)

C:\Users\phane\AppData\Local\Temp\ipykernel_21116\1694729012.py:1: FutureWarning: Passing literal json to 'read_


json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'String
IO' object.
data=pd.read_json(x)

data

Name Age

0 p1 21

1 p2 23

x2=data.to_json(orient="index")

x2

'{"0":{"Name":"p1","Age":21},"1":{"Name":"p2","Age":23}}'

pd.read_json(x2,orient="index")

C:\Users\phane\AppData\Local\Temp\ipykernel_21116\1222398037.py:1: FutureWarning: Passing literal json to 'read_


json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'String
IO' object.
pd.read_json(x2,orient="index")
Name Age

0 p1 21

1 p2 23

x3 =data.to_json(orient="columns")

x3

'{"Name":{"0":"p1","1":"p2"},"Age":{"0":21,"1":23}}'

pd.read_json(x3,orient="columns")

C:\Users\phane\AppData\Local\Temp\ipykernel_21116\2742886167.py:1: FutureWarning: Passing literal json to 'read_


json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'String
IO' object.
pd.read_json(x3,orient="columns")
Name Age

0 p1 21

1 p2 23

x4 = data.to_json(orient="values")

x4

'[["p1",21],["p2",23]]'

pd.read_json(x4,orient="values")

C:\Users\phane\AppData\Local\Temp\ipykernel_21116\2664431613.py:1: FutureWarning: Passing literal json to 'read_


json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'String
IO' object.
pd.read_json(x4,orient="values")
0 1

0 p1 21

1 p2 23

x5=data.to_json(orient="split")

x5

'{"columns":["Name","Age"],"index":[0,1],"data":[["p1",21],["p2",23]]}'

pd.read_json(x5,orient="split")

C:\Users\phane\AppData\Local\Temp\ipykernel_21116\426387458.py:1: FutureWarning: Passing literal json to 'read_j


son' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringI
O' object.
pd.read_json(x5,orient="split")
Name Age

0 p1 21

1 p2 23

Semi-Structured Json file


x = {"Name":"p1","marks":21} #semi_structure takes the format in dictionary
# whereas structured json takes the format in string

{'Name': 'p1', 'marks': 21}

type(x)

dict

x = {"Name":"p1","marks":{"Maths":21,"Hindi":20}}

pd.json_normalize(x)

Name marks.Maths marks.Hindi

0 p1 21 20

x = {"Name":"p1","marks":{"Sem1":{"Maths":21,"Hindi":20},"Sem2":{"Maths":22,"Hindi":25}}}

pd.json_normalize(x)

Name marks.Sem1.Maths marks.Sem1.Hindi marks.Sem2.Maths marks.Sem2.Hindi

0 p1 21 20 22 25

pd.json_normalize(x,max_level=0)

Name marks

0 p1 {'Sem1': {'Maths': 21, 'Hindi': 20}, 'Sem2': {...

pd.json_normalize(x,max_level=1)

Name marks.Sem1 marks.Sem2

0 p1 {'Maths': 21, 'Hindi': 20} {'Maths': 22, 'Hindi': 25}

pd.json_normalize(x,max_level=2)

Name marks.Sem1.Maths marks.Sem1.Hindi marks.Sem2.Maths marks.Sem2.Hindi

0 p1 21 20 22 25

x = {"Name":{"p1":12},"marks":[{"Sem1":20,"Sem2":30}],"Age":{"a":{"b":1}}}

pd.json_normalize(x,record_path=["marks"],meta=[["Name","p1"],["Age","a","b"]])
Sem1 Sem2 Name.p1 Age.a.b

0 20 30 12 1

Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like