Home > Enterprise >  Parse the inside json data of all variables
Parse the inside json data of all variables

Time:01-08

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

CodePudding user response:

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'])

CodePudding user response:

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"}

  • Related