Remove all xmlns tag from json file

Viewed 69

I try to remove all xmlns tag from my json file. How to do it only with xmltodict ?

    import xmltodict
    import json

        A = xmltodict.parse(open('JeuxTestv2.gml').read(), attr_prefix='')

with open('test.json', "wt", encoding='utf-8', errors='ignore') as f:
   json.dump(dict, f, indent=4, sort_keys=True)

Json file :

{
        "product A": {
            "nature": "03",
            "product": "aaaa",
            "qualiteCategorisation": "01",
           }
        "xmlns:fn": "http://www.w3.org/2005/xpath-functions",
        "xmlns:xs": "http://www.w3.org/2001/XMLSchema"
    },
2 Answers

Something like this:

In [3204]: d1 = { 
      ...:         "product A": { 
      ...:             "nature": "03", 
      ...:             "product": "aaaa", 
      ...:             "qualiteCategorisation": "01", 
      ...:            }, 
      ...:         "xmlns:fn": "http://www.w3.org/2005/xpath-functions", 
      ...:         "xmlns:xs": "http://www.w3.org/2001/XMLSchema" 
      ...:     } 

In [3210]: l = [x for x in d1.keys() if 'xmlns' in x]

In [3211]: for i in l: 
      ...:     d1.pop(i) 
      ...:                      

In [3212]: d1 
Out[3212]: 
{'product A': {'nature': '03',
  'product': 'aaaa',
  'qualiteCategorisation': '01'}}

Here is a one-liner using a dict-comprehension

In[1]: {k:v for k,v in d1.items() if "xmlns" not in k}
Out[2]: 
{'product A': {'nature': '03',
  'product': 'aaaa',
  'qualiteCategorisation': '01'}}
Related