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

Question:

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.

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?

Asked By: slic

||

Answers:

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.

Answered By: Michal Racko
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.