Home > Software design >  Get single data recordsfrom JSON in Python
Get single data recordsfrom JSON in Python

Time:01-05

I have a CSV file which I am converting to a JSON file using Python. This works so far. Now I want to output single data from the JSON file. I can open the file, read and access individual records but not everything is output.

Sorry for my bad English I hope you understand my request.

My JSON file

    "15689": {
        "Ger. Var.": "TG uni 1",
        "Ger.-ID": "15689",
        "Ger. Bez.": "SIGUSTO",
        "Norm": "DIN VDE 0701-0702",
        "SKL": " Ie ",
        "Iso-Messung": " nein",
        "Messverf.": "differenz",
        "Typ Anw.-Teil": "",
        "Geh\ufffduse OK": " ja  ",
        "Anschlussl. OK": " ja  ",
        "Aufschriften OK": " ja  ",
        "sonst. Teile OK": " ja  ",
        "Messung1": "Schutzleiterwiderstand:",
        "Messwert1": "0,181 Ohm",
        "Messwert1 zus.": "",
        "Grenwert MW1": "< 0,30 Ohm",
        "Messung2": "Schutzleiterstrom:",
        "Messwert2": "1,151 mA",
        "Messwert2 zus.": "",
        "Grenwert MW2": "< 10,00 mA",
        "Messung3": "",
        "Messwert3": "",
        "Messwert3 zus.": "",
        "Grenwert MW3": "",
        "Messung4": "",
        "Messwert4": "",
        "Messwert4 zus.": "",
        "Grenwert MW4": "",
        "Messung5": "",
        "Messwert5": "",
        "Messwert5 zus.": "",
        "Grenwert MW5": "",
        "Messung6": "",
        "Messwert6": "",
        "Messwert6 zus.": "",
        "Grenwert MW6": "",
        "Messung7": "",
        "Messwert7": "",
        "Messwert7 zus.": "",
        "Grenwert MW7": "",
        "Netzspannung": "236 V",
        "Strom": "0,20 A",
        "Wirkleistung": "29 W",
        "Scheinleistung": "47 VA",
        "Blindleistung": "37 Var",
        "Leistungsfaktor": "-0,62 IND.",
        "Netzfrequenz": "50,0 Hz",
        "Funtiontest OK": " ja  ",
        "Kundennummer": "ZF GOTHA",
        "Pr\ufffdfergebnis": " ja  ",
        "verw. Pr\ufffdfger\ufffdt": "TG uni 1",
        "Pr\ufffdfdatum": "20.12.2022",
        "Pr\ufffdfer": "KOBER-528",
        "Seriennr. Pr\ufffdfger\ufffdt": "M2100926"
    },
    "10168": {
        "Ger. Var.": "TG uni 1",
        "Ger.-ID": "10168",
        "Ger. Bez.": "SN48",
        "Norm": "DIN VDE 0701-0702",
        "SKL": " I  ",
        "Iso-Messung": " nein",
        "Messverf.": "differenz",
        "Typ Anw.-Teil": "",
        "Geh\ufffduse OK": " ja  ",
        "Anschlussl. OK": " ja  ",
        "Aufschriften OK": " ja  ",
        "sonst. Teile OK": " ja  ",
        "Messung1": "Schutzleiterwiderstand:",
        "Messwert1": "0,115 Ohm",
        "Messwert1 zus.": "",
        "Grenwert MW1": "< 0,30 Ohm",
        "Messung2": "Schutzleiterstrom:",
        "Messwert2": "0,750 mA",
        "Messwert2 zus.": "",
        "Grenwert MW2": "< 3,50 mA",
        "Messung3": "",
        "Messwert3": "",
        "Messwert3 zus.": "",
        "Grenwert MW3": "",
        "Messung4": "",
        "Messwert4": "",
        "Messwert4 zus.": "",
        "Grenwert MW4": "",
        "Messung5": "",
        "Messwert5": "",
        "Messwert5 zus.": "",
        "Grenwert MW5": "",
        "Messung6": "",
        "Messwert6": "",
        "Messwert6 zus.": "",
        "Grenwert MW6": "",
        "Messung7": "",
        "Messwert7": "",
        "Messwert7 zus.": "",
        "Grenwert MW7": "",
        "Netzspannung": "235 V",
        "Strom": "0,23 A",
        "Wirkleistung": "47 W",
        "Scheinleistung": "54 VA",
        "Blindleistung": "26 Var",
        "Leistungsfaktor": "0,87 IND.",
        "Netzfrequenz": "50,0 Hz",
        "Funtiontest OK": " ja  ",
        "Kundennummer": "ZF GOTHA",
        "Pr\ufffdfergebnis": " ja  ",
        "verw. Pr\ufffdfger\ufffdt": "TG uni 1",
        "Pr\ufffdfdatum": "20.12.2022",
        "Pr\ufffdfer": "KOBER-528",
        "Seriennr. Pr\ufffdfger\ufffdt": "M2100926"
    },

The Python File

import json
  
## JSON file
f = open ('C:/Users/user/Desktop/test.json', "rb")
  
# Reading from file
  
# Iterating through the json
# list
data = json.load(f, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None)

for i in data['15689']:
    print(i)

# Closing file
f.close()

I am searching for a number (e.g. "15689") in the JSON file and would like to see the related entries displayed. Unfortunately, only the first data is displayed and not the associated values.

The Output...

.
.
.
Ger. Var.
Ger.-ID 
Ger. Bez.
Norm
SKL
Iso-Messung
Messverf.
Typ Anw.-Teil
Geh�use OK
Anschlussl. OK
Aufschriften OK
sonst. Teile OK
Messung1
.
.
.

CodePudding user response:

You are printing the keys instead of the values, you can change the print line like this:

for i in data['15689']:
    print(data['15689'][i])  # add i to print both the keys and the values

or:

for k, v in data['15689'].items():
    print(v)  # add k to print both the keys and the values
  • Related