Python Forum
[split] Parse Nested JSON String in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Parse Nested JSON String in Python
#1
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:


Output:
{"comment":"This is an early endpoint to dCw, and is intended for experimental use. In addition to the JSON format, there are also lab projects for JSON-LD and XML.","item":{"type":"Human-Made_Object","id":"121392","uri":null,"workID":"1975.163","identified_by":[{"Appellation":{"has_type":null,"label":[{"value":"St. Georg - Schrank","language":"de"}],"preferred":"y"}}],"is_subject_to":[{"Right":{"has_type":{"label":[{"value":null}]}}}],"member_of_collection":[{"uri":null,"label":[{"value":"Moderne"}]}],"referred_to_by":[{"LinguisticObject":[{"has_type":{"uri":null,"label":[{"value":"Aussage zu Objektbezeichnung"}],"has_type":{"uri":"http:\/\/digicult.vocnet.org\/terminology\/ter01614"}},"content":{"value":"Schrank"}}]},{"LinguisticObject":[{"has_type":{"uri":null,"label":[{"value":"Aussage zu Epoche\/Stil"}],"has_type":{"uri":"http:\/\/digicult.vocnet.org\/terminology\/ter01614"}},"content":{"value":"Historismus"}}]},{"LinguisticObject":[{"has_type":{"id":"mat_tech_import","label":{"value":"Material\/Technik(import)"}},"content":{"value":"Eichenholz, geschnitzt, dunkel gebeizt, Eisenbeschl\u00e4ge","language":"de"}}]},{"LinguisticObject":[{"has_type":{"id":"description","label":{"value":"Beschreibung"}},"content":{"value":"Viert\u00fcriger Schrank, die Front mit Figuren in Nischen nahezu vollplastisch beschnitzt. Die Fassade horizontal in drei Zonen gegliedert. Alle Darstellungen in Nischen mit spiralig gedrehten S\u00e4ulen, Korbb\u00f6gen und Kreuzgew\u00f6lben. In der mittleren Zone zwei Schubladen mit vier Rechteckfeldern.","language":"de"}}]},{"LinguisticObject":[{"has_type":{"id":"description_internal","label":{"value":"interne Bemerkung"}},"content":{"value":" ","language":"de"}}]},{"LinguisticObject":[{"has_type":{"id":"additional_entry","label":{"value":"Zusatzangaben"}},"content":{"value":"Geschenk von Frau Erna Strelow, Bonn - Bad Godesberg","language":"de"}}]},{"LinguisticObject":[{"has_type":{"id":"literature","label":{"value":"Literatur"}},"content":{"value":"Jedding, 1977, S. 59, Kat. Nr 80 mit Abb.","language":"de"}}]},{"LinguisticObject":[{"has_type":{"id":"import","label":{"value":"Import"}},"content":{"value":"DS_Erstellung : 31.05.2002\nDS_Aenderung: 17.07.2014\nDS_AenderungUser: Administrator\nAnzahl Fotos: 1\n","language":"de"}}]},{"LinguisticObject":[{"has_type":{"id":"buzzwords","label":{"value":"Schlagworte"}},"content":{"value":"Majolika","language":"de"}}]}],"has_object_work_type":[{"uri":"http:\/\/obg.vocnet.org\/00000883","label":[{"value":"Schrank","qualifier":null,"role":"preferred","language":"de"}],"inScheme":{"uri":"http:\/\/obg.vocnet.org","label":{"value":"Objektbezeichnung"}}}],"has_type":[{"comment":"Epoche\/Stil to event, Darst.\/Ikonogr. to subject","uri":"http:\/\/digicult.vocnet.org\/sachgruppe\/1.373","label":[{"value":"Schrankm\u00f6bel","qualifier":null,"role":"preferred","language":"de"}],"inScheme":{"uri":"http:\/\/digicult.vocnet.org\/sachgruppe","label":{"value":"Sachgruppe"}}},{"comment":"Epoche\/Stil to event, Darst.\/Ikonogr. to subject","uri":"http:\/\/digicult.vocnet.org\/stil\/601.57","label":[{"value":"Historismus","qualifier":null,"role":"preferred","language":"de"}],"inScheme":{"uri":"http:\/\/digicult.vocnet.org\/stil","label":{"value":"Epoche\/Stil"}}},{"comment":"Epoche\/Stil to event, Darst.\/Ikonogr. to subject","uri":"http:\/\/hamburg.vocnet.org\/technik\/tech00051","label":[{"value":"gebeizt","qualifier":null,"role":"preferred","language":"de"}],"inScheme":{"uri":"http:\/\/hamburg.vocnet.org\/technik","label":{"value":"Technik"}}},{"comment":"Epoche\/Stil to event, Darst.\/Ikonogr. to subject","uri":"http:\/\/hamburg.vocnet.org\/technik\/tech00027","label":[{"value":"geschnitzt","qualifier":null,"role":"preferred","language":"de"}],"inScheme":{"uri":"http:\/\/hamburg.vocnet.org\/technik","label":{"value":"Technik"}}}],"consists_of":[{"uri":"http:\/\/hamburg.vocnet.org\/material\/mat00311","label":[{"value":"Eiche","qualifier":null,"role":"preferred","language":"de"}],"inScheme":{"uri":"http:\/\/hamburg.vocnet.org\/material","label":{"value":"Material"}}}],"was_present_at":[{"Activity":{"id":"354265","has_type":{"uri":"http:\/\/terminology.lido-schema.org\/lido00003","label":[{"value":"Ereignis"}]},"had_participant":[{"actor":{"label":[{"value":null}]},"role_actor":[{"id":"empty","uri":"","label":[{"value":""}]}],"attributionQualifierActor":null}],"has_time-span":{"begin_of_the_begin":"1890","end_of_the_end":"1900"},"took_place_at":[{"Appellation":{"label":[{"value":"Deutschland"}]},"has_note":[{"value":"Rheinland"}]}],"referred_to_by":[{"LinguisticObject":{"has_type":{"id":"eventTypeImportHelp","label":[{"value":"Import-Helper"}]},"content":[{"value":"Entwurf"}]}}]}},{"Activity":{"id":"354266","has_type":{"uri":"http:\/\/digicult.vocnet.org\/terminology\/ter01340","label":[{"value":"Zugang"}]},"had_participant":[{"actor":{"label":[{"value":null}]},"role_actor":[{"id":"vendor","uri":"","label":[{"value":"Zugang von"}]}],"attributionQualifierActor":null}],"has_time-span":{"begin_of_the_begin":"","end_of_the_end":""}}}],"inscription":[{"has_type":{"uri":"http:\/\/digicult.vocnet.org\/terminology\/","label":{"value":null}},"content":{"value":""},"position":{"label":{"value":""}}}],"object_count":1,"object_part":[{"has_type":{"id":"overall"},"has_dimension":[{"has_type":{"id":"height"},"has_unit":{"id":"cm"},"value":null,"comment":null},{"has_type":{"id":"width"},"has_unit":{"id":"cm"},"value":null,"comment":null}]}],"resource":[{"link_resource":{"has_type":{"uri":"http:\/\/terminology.lido-schema.org\/lido00481"},"value":"https:\/\/transformation.digicult-verbund.de\/object\/lido\/web-2-lido\/components\/gate\/file\/0083d5c81fe996d37914a96a4b5b5371","value_internal":"https:\/\/digicult-web.digicult-verbund.de\/entity-resources\/images\/digicult-web-mkg\/1\/1975.163.jpg","upload_file_name":"1975.163.jpg"},"title":null,"creator":null,"rights":{"has_type":{"uri":null}},"public":"no"}],"itemListElement":[{"has_type":{"id":"UpdateAction"},"agent":{"has_type":{"id":"Person"},"id":"3"},"endTime":"2020-10-26 17:14:45"},{"has_type":{"id":"UpdateAction"},"agent":{"has_type":{"id":"Person"},"id":"39"},"endTime":"2017-11-13 17:50:44"}],"runtime_initial":0.3274190425872803}}
Reply
#2
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.
Reply
#3
Error:
Reply
#4
(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)
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I parse the string? anna17 4 332 Apr-10-2024, 10:26 AM
Last Post: DeaD_EyE
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 202 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  parse json field from csv file lebossejames 4 765 Nov-14-2023, 11:34 PM
Last Post: snippsat
  doing string split with 2 or more split characters Skaperen 22 2,554 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,150 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Replace string in a nested Dictianory. SpongeB0B 2 1,217 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  parse String jaykappy 2 767 Dec-23-2022, 07:42 AM
Last Post: praveencqr
  Read nested data from JSON - Getting an error marlonbown 5 1,383 Nov-23-2022, 03:51 PM
Last Post: snippsat
  python read iperf log and parse throughput jacklee26 4 2,799 Aug-27-2022, 07:04 AM
Last Post: Yoriz
  Trying to parse only 3 key values from json file cubangt 8 3,504 Jul-16-2022, 02:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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