0

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",
          }
       }
    }
 ],
}
1
  • 1
    you can use del a["7"] or a.pop("7", None) Commented Mar 17, 2020 at 5:34

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.