admin管理员组

文章数量:1302329

I have imported a JSON file in Python for extrapolate the information that I have in it for selecting every single data or principal data that contains different data. When I try to select the principal ones, to see all the data that contains, I can't see it, but instead I only see the first data of the principal paragraph.

How this is possible? Is it impossible to see the data transformed in a table?

For example, I have this index with this data but I can't see all the data that this index contains when I recall AccessLevels from JSON with code like this:

Accesso = df['AccessLevels']
print(Accesso)

JSON:

"AccessLevels": {
    "Home.btnBuzzer": 0,
    "Home.btnLogin": 0,
    "Home.btnLogout": 0,
    "Home.btnMenu": 0,
    "L2A_ConfEdit.btnRecSave": 2,
    "L2A_ConfEdit.cbNameConf": 4,
    "L2A_ConfEdit.editParameters": 4,
    "L2B_ProgEdit.btnRecActivate": 1,
    "L2B_ProgEdit.btnRecSave": 2,
    "L2B_ProgEdit.btnRecSaveAs": 2,
    "L2B_ProgEdit.cbNameProg": 1,
    "L2B_ProgEdit.editParameters": 2,
    "L2E_Stats.btnRstPar": 1,
    "L2E_Stats.btnRstTot": 2,
    "L2_Menu.btnConfEdit": 0,
    "L2_Menu.btnMaint": 4,
    "L2_Menu.btnProgEdit": 0,
    "L2_Menu.btnRtCmds": 0,
    "L2_Menu.btnStats": 0,
    "L2_Menu.btnTestControl": 2
},

The result of printing the access level is this:

Home.btnBuzzer  0.0 
Home.btnLogin   0.0 
Home.btnLogout  0.0 
Home.btnMenu    0.0 
L2A_ConfEdit.btnRecSave  2.0 
  ... 
5    NaN
6    NaN
7    NaN 
8    NaN 
9    NaN
Name: AccessLevels, Length: 74, dtype: float64

Note that the JSON file is containing about 12k lines

import json
import pandas as pd

# Replace 'your_file.json' with the path to your JSON file
with open('00_000000065.json', 'r') as file:
    data = json.load(file)

#print whole json file
#print(data)


df = pd.DataFrame(data)
#Info = df['Info']['CPU12 prog code']
#print(Info)
#print("\nPrint the whole json file as a db to see how it works")    
#print(df)

print("\nPrinting the recipes\n")
#Ricetta = df['Recipes']
#Step = df['Recipes']['Programs']['13']['Step']['007']
#print(f"Print me step {Step}")

Info = df['Info']['CPU12 prog code']
print(Info)

Image = df['zImages']['0']
print(Image)


Step1 = df['Recipes']['Configs']['00']['Step']

print(Step1)

print("\nTest of the various levels\n")
Accesso = df['AccessLevels']
print(Accesso)

print("\nPrint of the counter\n")
Contatore = df['CounterEnabled']
print(Contatore)

I have imported a JSON file in Python for extrapolate the information that I have in it for selecting every single data or principal data that contains different data. When I try to select the principal ones, to see all the data that contains, I can't see it, but instead I only see the first data of the principal paragraph.

How this is possible? Is it impossible to see the data transformed in a table?

For example, I have this index with this data but I can't see all the data that this index contains when I recall AccessLevels from JSON with code like this:

Accesso = df['AccessLevels']
print(Accesso)

JSON:

"AccessLevels": {
    "Home.btnBuzzer": 0,
    "Home.btnLogin": 0,
    "Home.btnLogout": 0,
    "Home.btnMenu": 0,
    "L2A_ConfEdit.btnRecSave": 2,
    "L2A_ConfEdit.cbNameConf": 4,
    "L2A_ConfEdit.editParameters": 4,
    "L2B_ProgEdit.btnRecActivate": 1,
    "L2B_ProgEdit.btnRecSave": 2,
    "L2B_ProgEdit.btnRecSaveAs": 2,
    "L2B_ProgEdit.cbNameProg": 1,
    "L2B_ProgEdit.editParameters": 2,
    "L2E_Stats.btnRstPar": 1,
    "L2E_Stats.btnRstTot": 2,
    "L2_Menu.btnConfEdit": 0,
    "L2_Menu.btnMaint": 4,
    "L2_Menu.btnProgEdit": 0,
    "L2_Menu.btnRtCmds": 0,
    "L2_Menu.btnStats": 0,
    "L2_Menu.btnTestControl": 2
},

The result of printing the access level is this:

Home.btnBuzzer  0.0 
Home.btnLogin   0.0 
Home.btnLogout  0.0 
Home.btnMenu    0.0 
L2A_ConfEdit.btnRecSave  2.0 
  ... 
5    NaN
6    NaN
7    NaN 
8    NaN 
9    NaN
Name: AccessLevels, Length: 74, dtype: float64

Note that the JSON file is containing about 12k lines

import json
import pandas as pd

# Replace 'your_file.json' with the path to your JSON file
with open('00_000000065.json', 'r') as file:
    data = json.load(file)

#print whole json file
#print(data)


df = pd.DataFrame(data)
#Info = df['Info']['CPU12 prog code']
#print(Info)
#print("\nPrint the whole json file as a db to see how it works")    
#print(df)

print("\nPrinting the recipes\n")
#Ricetta = df['Recipes']
#Step = df['Recipes']['Programs']['13']['Step']['007']
#print(f"Print me step {Step}")

Info = df['Info']['CPU12 prog code']
print(Info)

Image = df['zImages']['0']
print(Image)


Step1 = df['Recipes']['Configs']['00']['Step']

print(Step1)

print("\nTest of the various levels\n")
Accesso = df['AccessLevels']
print(Accesso)

print("\nPrint of the counter\n")
Contatore = df['CounterEnabled']
print(Contatore)
Share edited Feb 14 at 19:34 CcmU 1,01014 silver badges30 bronze badges asked Feb 11 at 3:51 gebgeb 11 bronze badge 1
  • I run your code and it works fine, are you sure that the json is correctly loaded? There is the possibility it's corrupted? Also, there is a specific reason why you don't use the pandas builtin pd.read_json()? – CcmU Commented Feb 11 at 11:07
Add a comment  | 

1 Answer 1

Reset to default 0
import json
import pandas as pd

# Load file JSON
with open('00_000000065.json', 'r') as file:
    data = json.load(file)

# Print file JSON
print(data)

# Create DataFrame and set Key like index
access_levels = pd.DataFrame(list(data['AccessLevels'].items()), columns=['Key', 'Value'])
access_levels.set_index("Key", inplace=True)
#Stampa Tutto
print("\nAccessLevels:")
print(access_levels)

# Access the value directly using the key
print("Value of Home.btnBuzzer:")
print(access_levels.loc["Home.btnBuzzer", "Value"])

print("Ciao Amico, sono anch'io italiano. :D, spero di averti aiutato")

The problem is in the JSON, that is, when importing AccessLevels it contains a dictionary (a nested structure) and pandas does not flatten the JSON correctly, leading to non-direct access to the values.

To solve it, create the DataFrame transforming it into a two-column table, setting the Key column as an index so you access the value via key.

本文标签: Problem reading data while accessing json file in python with pandasStack Overflow