skip to Main Content

Im working on big json data . In every data set ther will be Name,Type,Value. Im actually need to parse the all type which as equal to "Shirt". I don’t know to parse the inside json data.


x={
  "Data": "Ecommerce",
  "Host":{
   "Items": [
    [
      {
        "Name": "cot",
        "Value": 99,
        "Type": "Shirt"
      },
      {
        "Name": "ploy",
        "Value": 90,
        "Type": "Pant"
      },
      {
        "Name": "lyc",
        "Value": 22,
        "Type":"Shirt"
      }
    ]
  ],
}}
k=x.get("Host")
print(k)

The above code will Display all data inside the Host.

What I’m trying to get output as parse only the Type: Shirt and Value of the Shirt .

I tried with some Def ,loop concepts but i can’t able to achieve my output.

Output I’m looking for :-
If Type = Shirt , need parse that json data

Cot:90
Lyc:22

In dict format

2

Answers


  1. This shows how to access the data. I assume you can change this to create a dictionary.

    for item in x['Host']['Items'][0]:
        if item['Type'] == 'Shirt':
            print(item['Name'],':',item['Value'])
    
    Login or Signup to reply.
  2. This is an example to do it with short for

    items = x["Host"]["Items"]
    shirt_values = {item["Name"]: item["Value"] for sublist in items for item in sublist if item["Type"] == "Shirt"}
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search