builtins.StopIteration: 2 and json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2) errors
Question:
I have a JSON file I am trying to import into MongoDB.
Using the python code from the [documentation][1], I get an error on myDict = json.loads(jsonObj)
(see traceback below). I have used an online tool to validate my JSON and it is marked as valid. The problem seems to be in line 2 but I have no idea what to do to make this work. Please help!
Python traceback:
obj, end = self.scan_once(s, idx)
builtins.StopIteration: 2
During handling of the above exception, another exception occurred:
File "/Users/s/Documents/s/importToMongo.py", line 13, in <module>
myDict = json.loads(jsonObj)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)```
**My JSON:**
```[
{
"field1": "6405441",
"field9": "326",
"field10": "12",
"field17": "1180",
"field22": "Avenue Brugmann",
"field23": "Brugmannlaan",
"field24": null,
"field28": "Uccle",
"field29": "Ukkel",
"field30": null,
"field13": "648301.355 666355.729"
},
{
"field1": "fid",
"field9": "housenumber",
"field10": "boxnumber",
"field17": "postal_info_objectid",
"field22": "streetname_fr",
"field23": "streetname_nl",
"field24": "streetname_de",
"field28": "municipality_fr",
"field29": "municipality_nl",
"field30": "municipality_de",
"field13": "position"
}
]```
[1]: https://www.mongodb.com/compatibility/json-to-mongodb
Answers:
Problem was this code only works for JSON objects with this format:
- No brackets ( [] )
- No commas at the end of the last field-value pair
- Only one object
Example:
{
"field1":"value",
"field2":"value"
}
I have a JSON file I am trying to import into MongoDB.
Using the python code from the [documentation][1], I get an error on myDict = json.loads(jsonObj)
(see traceback below). I have used an online tool to validate my JSON and it is marked as valid. The problem seems to be in line 2 but I have no idea what to do to make this work. Please help!
Python traceback:
obj, end = self.scan_once(s, idx)
builtins.StopIteration: 2
During handling of the above exception, another exception occurred:
File "/Users/s/Documents/s/importToMongo.py", line 13, in <module>
myDict = json.loads(jsonObj)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)```
**My JSON:**
```[
{
"field1": "6405441",
"field9": "326",
"field10": "12",
"field17": "1180",
"field22": "Avenue Brugmann",
"field23": "Brugmannlaan",
"field24": null,
"field28": "Uccle",
"field29": "Ukkel",
"field30": null,
"field13": "648301.355 666355.729"
},
{
"field1": "fid",
"field9": "housenumber",
"field10": "boxnumber",
"field17": "postal_info_objectid",
"field22": "streetname_fr",
"field23": "streetname_nl",
"field24": "streetname_de",
"field28": "municipality_fr",
"field29": "municipality_nl",
"field30": "municipality_de",
"field13": "position"
}
]```
[1]: https://www.mongodb.com/compatibility/json-to-mongodb
Problem was this code only works for JSON objects with this format:
- No brackets ( [] )
- No commas at the end of the last field-value pair
- Only one object
Example:
{
"field1":"value",
"field2":"value"
}