Why does pandas.json_normalize(json_results) raise a NotImplementedError?
Asked Answered
T

3

8

I have a json variable named json_results and I am running pandas.json_normalize(json_results). It raises the following error:

in _json_normalize
    raise NotImplementedError
NotImplementedError

How can I resolve this?

Trotta answered 20/8, 2021 at 15:48 Comment(1)
Please edit your question and provide a minimal reproducible example.Mullinax
T
10

This error can happen if you pass a JSON string to json_normalize, not an already decoded JSON object.

>>> import json
>>> import pandas as pd

>>> s = '{"hello": "world"}'
>>> pd.json_normalize(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.7/site-packages/pandas/io/json/_normalize.py", line 423, in _json_normalize
    raise NotImplementedError
NotImplementedError

>>> d = json.loads(s)
>>> pd.json_normalize(d)
   hello
0  world

If this is the case, use json.loads (or an equivalent Pandas function) first.

Tijuana answered 19/4, 2022 at 8:24 Comment(0)
S
1

Try from_records method like:

import json
import pandas as pd

s = '{"Column 1":{"Row 1":"Value 1","Row 2":"Value 2","Row 3":"Value 3"},' \
    '"Column 2":{"Row 1":"Value 4","Row 2":"Value 5","Row 3":"Value 6"}}'
result = pd.DataFrame.from_records(json.loads(s))
print(result)

Output:

      Column 1 Column 2
Row 1  Value 1  Value 4
Row 2  Value 2  Value 5
Row 3  Value 3  Value 6

info() method output:

<class 'pandas.core.frame.DataFrame'>
Index: 3 entries, Row 1 to Row 3
Data columns (total 2 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   Column 1  3 non-null      object
 1   Column 2  3 non-null      object
dtypes: object(2)
Sheedy answered 4/5, 2022 at 19:45 Comment(0)
F
0

Are you importing json_normalize like this:

from pandas.io.json import json_normalize

If so, import pandas and then try calling json_normalize like this:

pd.json_normalize()

Instead of:

json_normalize()

This is what fixed this issue for me.

Formenti answered 10/1, 2022 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.