Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I change the format of a JSON output from iterating through a python dictionary

i’m trying to change the format of a json file from:

{
    "ordnungsrufe": [
        {
            "date": "1961-04-18",
            "president": "Prof. D. Dr. Eugen Gerstenmaier (CDU/CSU)",
            "wp": "3",
            "protocol": "Protokoll der 154. Sitzung des 3. Deutschen Bundestages",
            "source": "https://dserver.bundestag.de/btp/03/03154.pdf",
            "calledOutName": "Adolf Ludwig, MdB",
            "calledOutParty": "SPD"
        }
    ]
}

to:

[
    {
        "calledOut": {
            "name": "Adolf Ludwig, MdB",
            "party": "SPD"
        },
        "date": "1961-04-18",
        "president": "Prof. D. Dr. Eugen Gerstenmaier (CDU/CSU)",
        "source": {
            "pdf": "https://dserver.bundestag.de/btp/03/03154.pdf",
            "sectionFrom": "",
            "sectionTo": "",
            "linkToVideo": ""
        }
    }
]

I’have build this python script, but I’m struggling to get the expected format.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import json
import codecs

data = json.load(codecs.open('data.json', 'r', 'utf-8-sig'))
#print(data)

ordnungsruf = {}

for person in data['ordnungsrufe']:
    ordnungsruf[person['calledOutName']] = {
        "calledOut" : {
            "name": person["calledOutName"],
            "party": person["calledOutParty"]
        },
        "date": person["date"],
        "president" : person["president"],
        "source" : {
          "pdf": person["source"],
          "sectionFrom": "",
          "sectionTo": "",
          "linkToVideo": "",
        }
    }

with open('ordnungsrufe_ordered.json', 'w') as json_file:
  json.dump(ordnungsruf, json_file, indent=2)

my output:

{
  "Adolf Ludwig, MdB": {
    "calledOut": {
      "name": "Adolf Ludwig, MdB",
      "party": "SPD"
    },
    "date": "1954-12-08",
    "president": "Prof. D. Dr. Eugen Gerstenmaier (CDU/CSU)",
    "source": {
      "pdf": "https://dserver.bundestag.de/btp/02/02059.pdf",
      "sectionFrom": "",
      "sectionTo": "",
      "linkToVideo": ""
    }
  },
}

I tried to iterate through the dictionary with several different methods and I’m always failing to get the right JSON. This example seems to be the closest.

Is there a better way to iterate through a dictionary to change the format of a python script?

>Solution :

Putting results into a list should work:

result = []
for item in data['ordnungsrufe']:
    result.append({
        'calledOut': {
            'name': item['calledOutName'],
            'party': item['calledOutParty']
        },
        'date': item['date'],
        'president': item['president'],
        'source': {
            'pdf': item['source'],
            'sectionFrom': '',
            'sectionTo': '',
            'linkToVideo': ''
        }
    })
print(result)

N.b. it might be a good idea to save the result as a dictionary:

with open('target.json', 'w') as f:
    json.dump({'items': result}, f)

as it will have been a bit more future proof.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading