![]() |
[split] Parse Nested JSON String in Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [split] Parse Nested JSON String in Python (/thread-39699.html) |
[split] Parse Nested JSON String in Python - mmm07 - Mar-28-2023 Hi! just saw this post and I think** it will help me with the same issue.. any chance you could help me? Thanks in advance!! my amended..code.. import os from pathlib import Path from copy import deepcopy import csv import json import sys class parse_index: def __init__(self): # files are in same path as script, assure path here. os.chdir(os.path.abspath((os.path.dirname(__file__))) filepath = Path('mlealcauston/downloads') jsonfile = downloads/'axel_export.json' with jsonfile.open() as fp: self.jindex = json.load(fp) self.csvfile = filepath / 'all_json.csv' def create_csv(self): header = ["type", "id", "uri", "workID", "identified_by", "is_subject_to", "member_of_collection","referred_to_by","has_type", "was_present_at","inscription", "object_count", "object_part"] base = self.jindex["item"] with self.csvfile.open('w') as cfptr: cwrite = csv.writer(cfptr, delimiter=',').writerow cwrite(header) seqno = 1 for key in base.keys(): # print(f"key: {key}") bk = base[key] for key1 in bk.keys(): bk1 = bk[key1] buffer1 = [] buffer1.append(bk1['type']) buffer1.append(bk1['id']) buffer1.append(bk1['uri']) buffer1.append(bk1['workID']) buffer1.append(bk1['identified_by']['Appellation']['has_type']) buffer1.append(bk1['identified_by']['label']['value']['language']) buffer1.append(bk1['identified_by']['preferred']) buffer1.append(bk1['identified_by']['preferred']) buffer1.append(bk1['is_subject_to']['Right']['has_type']['label']['value']) buffer1.append(bk1['member_of_collection']['uri']['has_type']['label']['value']) buffer1.append(bk1['member_of_collection']['label']['value']) buffer1.append(bk1['referred_to_by']['Linguistic_Object']['has_type']['uri']) buffer1.append(bk1['referred_to_by']['Linguistic_Object']['has_type']['label']['value']) buffer1.append(bk1['referred_to_by']['Linguistic_Object']['has_type']['has_type']['uri']) buffer1.append(bk1['referred_to_by']['Linguistic_Object']['content']['value']) buffer1.append(bk1['has_object_work_type']['uri']) buffer1.append(bk1['has_object_work_type']['label']['value']) buffer1.append(bk1['has_object_work_type']['label']['qualifier']) buffer1.append(bk1['has_object_work_type']['label']['role']) buffer1.append(bk1['has_object_work_type']['label']['language']) buffer1.append(bk1['has_object_work_type']['inScheme']['uri']) buffer1.append(bk1['has_object_work_type']['inScheme']['label']['value']) buffer1.append(bk1['has_type']['comment']) buffer1.append(bk1['has_type']['uri']) buffer1.append(bk1['has_type']['label']['value']) buffer1.append(bk1['has_type']['label']['qualifier']) buffer1.append(bk1['has_type']['label']['role']) buffer1.append(bk1['has_type']['label']['language']) buffer1.append(bk1['has_type']['inScheme']['uri']) buffer1.append(bk1['has_type']['inScheme']['label']['label']) buffer1.append(bk1['consists_of']['uri']) buffer1.append(bk1['consists_of']['label']['value']) buffer1.append(bk1['consists_of']['label']['qualifier']) buffer1.append(bk1['consists_of']['label']['role']) buffer1.append(bk1['consists_of']['label']['language']) buffer1.append(bk1['consists_of']['inScheme']['uri']) buffer1.append(bk1['consists_of']['inScheme']['label']['value']) buffer1.append(bk1['was_present_at']['Activity']['id']) buffer1.append(bk1['was_present_at']['Activity']['has_type']['uri']) buffer1.append(bk1['was_present_at']['Activity']['has_type']['label']['value']) buffer1.append(bk1['was_present_at']['Activity']['had_participant']['actor']['label']['value']) buffer1.append(bk1['was_present_at']['Activity']['had_participant']['role_actor']['id']) buffer1.append(bk1['was_present_at']['Activity']['had_participant']['role_actor']['uri']) buffer1.append(bk1['was_present_at']['Activity']['had_participant']['role_actor']['label']['value']) buffer1.append(bk1['was_present_at']['Activity']['had_participant']['attributionQualifierActor']) buffer1.append(bk1['was_present_at']['Activity']['has_time-span']['begin_of_the_begin']) buffer1.append(bk1['was_present_at']['Activity']['has_time-span']['end_of_the_end']) buffer1.append(bk1['was_present_at']['Activity']['referred_to_by']['LinguisticObject']['has_type']['id']) buffer1.append(bk1['was_present_at']['Activity']['referred_to_by']['LinguisticObject']['has_type']['label']['value']) buffer1.append(bk1['was_present_at']['Activity']['referred_to_by']['LinguisticObject']['content']['value']) buffer1.append(bk1['inscription']['has_type']['uri']) buffer1.append(bk1['inscription']['has_type']['label']['value']) buffer1.append(bk1['inscription']['content']['value']) buffer1.append(bk1['inscription']['position']['label']['value']) buffer1.append(bk1['object_count']) buffer1.append(bk1['object_part']['has_type']['id']) buffer1.append(bk1['object_part']['has_unit']['id']) buffer1.append(bk1['object_part']['has_unit']['value']) buffer1.append(bk1['object_part']['has_unit']['comment']) def main(): pi = parse_index() pi.create_csv() if __name__ == '__main__': main()** the dynamic information is confusing me at the moment.. Json below:
RE: [split] Parse Nested JSON String in Python - Larz60+ - Mar-28-2023 mmm07 Wrote:Hi! just saw this post and I think** it will help me with the same issue.. any chance you could help me? Thanks in advance!What issue, your goal is very vague (almost non-existent), please elaborate. [split] Parse Nested JSON String in Python2 - mmm07 - Mar-28-2023
RE: [split] Parse Nested JSON String in Python - mmm07 - Mar-28-2023 (Mar-28-2023, 11:07 AM)Larz60+ Wrote:mmm07 Wrote:Hi! just saw this post and I think** it will help me with the same issue.. any chance you could help me? Thanks in advance!What issue, your goal is very vague (almost non-existent), please elaborate. turning this nested json into tabular format (including the additional nested entries) (i have 300k jsons like this) RE: [split] Parse Nested JSON String in Python - snippsat - Mar-28-2023 (Mar-28-2023, 04:04 PM)mmm07 Wrote: turning this nested json into tabular format (including the additional nested entries)Take a look this post where i use json_normalize .Can also look at this blog post. |