0

I try to read data from my json file. First I convert my xml file to json and I get in output this json file for example :

  [
        {
            "ABC": {

                "idObjet": "AffleurantEnveloppePCRS.0",
                "nature": "03",
                "precisionPlanimetrique": "010",

                },
                "reseau": "DECH",
                "thematique": "10"
            },
            "xmlns:fn": "http://www.w3.org/2005/xpath-functions",
        },
        {
            "DEF": {
                "enveloppe": {
                    "xlink:href": "GEOMETRIE.ENV.AffleurantEnveloppePCRS.0"
                },
                "gml:id": "GEOMETRIE.AffleurantEnveloppePCRS.0"
            },
            "xmlns:fn": "http://www.w3.org/2005/xpath-functions",
        }
]

Then I try to print some data from this file. I use json.loads() to read this data:

 import xmltodict
 import json


 dict = xmltodict.parse(open('Testv2.gml').read(), attr_prefix='')

 dict3 = json.loads('test.json', strict=False)

 print(dict3.keys())

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

I try to print only keys (ABC and DEF)

ERROR :

Traceback (most recent call last):
  File "C:/test.py", line 7, in <module>
    dict3 = json.loads('test.json', strict=False)
  File "C:\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 370, in loads
    return cls(**kw).decode(s)
  File "C:\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
martin
  • 143
  • 1
  • 3
  • 9
  • 4
    the json you posted is invalid. at least there is no several symbol `]` in the end. You can try online json validators to check json validity to fix json file before `joads` usage – ncopiy Jun 12 '20 at 23:40
  • 2
    [`json.loads()`](https://docs.python.org/3/library/json.html#json.loads) expects the first argument passed to it to be a string (or type `bytes` or `bytearray`) with JSON in it, not a filename, I think you want [`json.load()`](https://docs.python.org/3/library/json.html#json.load). Unrelated, but you should **never** name a variable the same as a Python built-in, like `dict`. – martineau Jun 13 '20 at 00:25

0 Answers0