Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON Parsing
#1
I have a situation where i need to parse a JSON into relation tables. the JSON comprise of dictionaries, list with 4 -5 level nested. also, have a rules to combine multiple dicts or list into a single relation table. I have kind of started this with recursive logic but trying to see if we have any option to avoid recursive logic but still accomplish same.
same JSON : ( show the context of use case)
Emp : {
K1 : V1
Address : []
K2 : V2
job : {
K3 : v3
}
}
relation table map ==> Address will be a Address table, Emp table should have rest of the info.

Appreciate any of you share thoughts / code around this ?
Reply
#2
I'm not sure if I understood your question but python has a powerful json module.

import json

my_str_json = '{"Emp" : {"K1" : "V1", "Address" : [], "K2" : "V2", "job" : { "K3" : "v3"}}}'
my_json = json.loads(my_str_json)
print(my_json["Emp"]["job"]["K3"]) # 'v3'
Reply
#3
Thanks for the reply.

import json

my_str_json = '{"Emp" : {"K1" : "V1", "Address" : [], "K2" : "V2", "job" : { "K3" : "v3"}}}'
my_json = json.loads(my_str_json)
print(my_json["Emp"]["job"]["K3"]) # 'v3'

I was trying to make JSON parsing as meta data ( configuration) driven. As you see in the above code, in order to get value of "K3", we have slicing rule as my_json["Emp"]["job"]["K3"]. instead of hard coding, i was thinking of passing slicing rule from a config file and python code reads the config and apply slicing rule on respective python dict.
eg:
Config file :
rule1 : ["Emp"]["job"]["K3"]

in the code:
import json

my_str_json = '{"Emp" : {"K1" : "V1", "Address" : [], "K2" : "V2", "job" : { "K3" : "v3"}}}'
my_json = json.loads(my_str_json)
print(my_json[rule1]) # 'v3'

when i tried that, im getting KeyError though respective key present in the dictionary. but the same is working for single dimension slicing but not multi dim.
Appreciate if you guys share some light whether its really possible?
Reply
#4
Just an initial idea...

import json


def get_value(idx, d):
    if isinstance(d, dict):
        if my_keys[idx] in d:
            return get_value(idx + 1, d[my_keys[idx]])
    else:
        return d
    return "Not found!"


my_str_json = '{"Emp" : {"K1" : "V1", "Address" : [], "K2" : "V2", "job" : { "K3" : "v3"}}}'
my_json = json.loads(my_str_json)
my_keys = ["Emp", "job", "K3"] # you'll have to create this list from the config file
value = get_value(0, my_json)
print(value)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing large JSON josvink66 5 669 Jan-10-2024, 05:46 PM
Last Post: snippsat
  Parsing JSON pyStund 4 3,004 Jul-31-2022, 02:02 PM
Last Post: pyStund
  Json Parsing sshree43 5 1,801 May-04-2022, 09:21 PM
Last Post: snippsat
  json api data parsing elvis 0 934 Apr-21-2022, 11:59 PM
Last Post: elvis
  string indices must be integers when parsing Json ilknurg 3 6,390 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  Help Parsing JSON kfwydfo1x 5 4,601 Jan-26-2021, 10:42 AM
Last Post: DeaD_EyE
  Parsing JSON with backslashes bazcurtis 3 9,337 Feb-08-2020, 01:13 PM
Last Post: bazcurtis
  JSON parsing (nested) fakka 0 3,079 Nov-25-2019, 09:25 PM
Last Post: fakka
  Parsing json - dictionary with in dictionary krish216 6 3,655 Jul-30-2019, 10:01 PM
Last Post: cvsae

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020