Home > Software design >  How to parse Nested Json with python adjacent keys inside list
How to parse Nested Json with python adjacent keys inside list

Time:06-07

I am trying to parse this json and new in json. Input

data ={'11': {'Des Content': ['Update Version', '11: Date of User'],
  'Date': ['920221207'],
  'service': ['courier']}}
keys=["11","21","13"]

Output

{
"11:":{
     "Describe" : "Date of User",
     "Data" :{
     "Date":["920221207"],
     "service":["courier"]
  }   
}

CodePudding user response:

You probably looking for this

import json

js = {'11': {'Des Content': ['Update Version', '11: Date of User'],
  'Date': ['920221207'],
  'service': ['courier']}}

print(json.dumps(js, indent=2))

output:

{
  "11": {
    "Des Content": [
      "Update Version",
      "11: Date of User"
    ],
    "Date": [
      "920221207"
    ],
    "service": [
      "courier"
    ]
  }
}

CodePudding user response:

Try this

res = {}
for k,v in data.items():
    res[k] = {"Describe": v.pop("Des Content")[-1].split(": ")[-1], # remove Des Content and assign Date of User to Describe
              "Data": v} # assign the remaining v dict to Data
res
# {'11': {'Describe': 'Date of User', 
#         'Data': {'Date': ['920221207'], 'service': ['courier']}}}
  • Related