Giorgia Mancini
Giorgia Mancini

Reputation: 131

Group values in json file

I have a json file like this:

{
  "PMID": {
    "0": 31920716.0,
    "1": 31915546.0,
    "2": 31912059.0,
    "3": 31908815.0
},
"ArticleTitle": {
    "0": "Clinical Usefulness of Computational Modeling-Guided Persistent Atrial Fibrillation Ablation: Updated Outcome of Multicenter Randomized Study.",
    "1": "Prevalence and Progression of Cognitive Impairment in Atrial Fibrillation Patients after Treatment with Catheter Ablation or Drug Therapy.",
    "2": "Non traumatic spinal epidural haematoma.",
    "3": "Twelve-month outcome in patients with stroke and atrial fibrillation not suitable to oral anticoagulant strategy: the WATCH-AF registry."
},
 "Authors": {
    "0": [
      "Kim IS",
      "Uhm JS",
    ],
    "1": [
      "Ortak J",
      "Caglayan E"
    ],
    "2": [
      "Khalid NA",
      "Shah N"
    ],
    "3": [
      "Nighoghossian N",
      "Amarenco P"
    ]
  }
}

and I want to group values in this way:

{
"0": {
       "PMID": 31920716.0,
       "ArticleTitle": "Clinical Usefulness of Computational Modeling-Guided Persistent Atrial Fibrillation Ablation: Updated Outcome of Multicenter Randomized Study.",
       "Authors" : [
      "Kim IS",
      "Uhm JS",
    ],
}

and so on, by the position in the dictionary in order to obtain an item with all the indexes grouped. Is there a function to do this operation in Python?

Upvotes: 0

Views: 54

Answers (1)

Sarath Sadasivan Pillai
Sarath Sadasivan Pillai

Reputation: 7091

You can create a dictionary and keep updating in the order you like to keep it . The following code explains it

def pack_ordinal(data):
    result = dict()
    for i in data:
        for j in data[i]:
            result[j] = result.get(j, dict())
            result[j][i] = data[i][j]
    return result

Upvotes: 1

Related Questions