Shi Jie Tio
Shi Jie Tio

Reputation: 2529

How to used python filter json string based on key that given in array form?

I have below json string:

a={"44":[
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"P3P34",
             "sum":"807",
          }
       }
    },
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"BFB",
             "sum":"8A",
          }
       }
        }
 ],
 "49":[
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"P3P34",
             "sum":"807",
          }
       }
    },
    {
       "17":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"BFB",
             "sum":"8A",
          }
       }
    }
 ],
 "7":[
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"P3P34",
             "sum":"807",
          }
       }
    },
    {
       "6":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"BFB",
             "sum":"0A",
          }
       }
    }
 ],
}

The key from above json string get by a.keys() is:

dict_keys(['44', '49', '7'])

How to filter the a so that it remain the key of 44 and 49 only if the array given is ['44', '49'], below is my expected output:

{"44":[
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"P3P34",
             "sum":"807",
          }
       }
    },
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"BFB",
             "sum":"8A",
          }
       }
        }
 ],
 "49":[
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"P3P34",
             "sum":"807",
          }
       }
    },
    {
       "17":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"BFB",
             "sum":"8A",
          }
       }
    }
 ],
}

Upvotes: 0

Views: 34

Answers (2)

GordonAitchJay
GordonAitchJay

Reputation: 4860

If I understand you correctly, you want to use a dict comprehension like this:

import json

filtered_json = {key: value for key, value in a.items() if key in ('44', '49')}
json_str = json.dumps(filtered_json , indent=4)
print(json_str)

Output:

{
    "44": [
        {
            "16": {
                "unitData": [],
                "unitHeader": {
                    "t9u": "P3P34",
                    "sum": "807"
                }
            }
        },
        {
            "16": {
                "unitData": [],
                "unitHeader": {
                    "t9u": "BFB",
                    "sum": "8A"
                }
            }
        }
    ],
    "49": [
        {
            "16": {
                "unitData": [],
                "unitHeader": {
                    "t9u": "P3P34",
                    "sum": "807"
                }
            }
        },
        {
            "17": {
                "unitData": [],
                "unitHeader": {
                    "t9u": "BFB",
                    "sum": "8A"
                }
            }
        }
    ]
}

Upvotes: 1

Abhishek Kulkarni
Abhishek Kulkarni

Reputation: 1767

Try this below:

    a={"44":[
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"P3P34",
             "sum":"807",
          }
       }
    },
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"BFB",
             "sum":"8A",
          }
       }
        }
 ],
 "49":[
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"P3P34",
             "sum":"807",
          }
       }
    },
    {
       "17":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"BFB",
             "sum":"8A",
          }
       }
    }
 ],
 "7":[
    {
       "16":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"P3P34",
             "sum":"807",
          }
       }
    },
    {
       "6":{
          "unitData":[

          ],
          "unitHeader":{
             "t9u":"BFB",
             "sum":"0A",
          }
       }
    }
 ],
}

given_array = ['44', '49']
    for i in list(a.keys()):
        if i not in given_array:
            a.pop(i)
print(a)

Upvotes: 1

Related Questions