r = requests.get(http_get_url, headers=headers)
r = r.text
r = r.replace("true", "True")
z = json.loads(r)
however, instead of loading the json (or python dict), I get:
Traceback (most recent call last):
File "/home/noir/PycharmProjects/Work_Projects/get_errors.py", line 21, in <module>
get_errors(id)
File "/home/noir/PycharmProjects/Work_Projects/get_errors.py", line 17, in get_errors
z = json.loads(r)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 26 (char 28)
The result of print(type(r)) is <class 'str'>
and the output of print(r) after converting 'true' to 'True' is:
{
"HasItemsWithCount": True,
"Collection": [
{
"GroupId" : "14",
"Time" : "5/16/18, 5:02 PM",
"File" : "[ESCAPE[]]",
"Message" : "[ESCAPE[Client was restarted during backup, session may be incomplete.]]",
"Count" : "3"
},
]
}
So I fail to understand why the correctly formatted string of r fails to import into json.
Also, if I take this output and write it directly into python via copy/paste, the type of the variable is dict, showing me that the text is formatted correctly for a dict in Python. So I'm not sure why json.loads fails.
Your replace is backwards:
Instead of:
r = r.replace("true", "True")
Try:
r = r.replace("True", "true")
Related
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"
}
I am trying to invoke
expect_column_values_to_match_json_schema
as per
https://legacy.docs.greatexpectations.io/en/latest/autoapi/great_expectations/dataset/dataset/index.html#great_expectations.dataset.dataset.Dataset.expect_column_values_to_match_json_schema
jschema = {
"type" : "object",
"properties" : {
"xyz" : {"type" : "string"}
}
}
df_ge.expect_column_values_to_match_json_schema(column='abc',json_schema=jschema,row_condition="a=='1' and b=='2' and c=='3'",condition_parser='pandas')
however i got this error
File "/some/path/lib/python3.7/site-packages/great_expectations/dataset/pandas_dataset.py", line 1554, in matches_json_schema
val_json = json.loads(val)
File "/usr/lib/python3.7/json/__init__.py", line 341, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not dict
so i tried
df_ge.expect_column_values_to_match_json_schema(column='abc',json_schema=json.loads(str(jschema)),row_condition="a=='1' and b=='2' and c=='3'",condition_parser='pandas')
but then i get
df_ge.expect_column_values_to_match_json_schema(column='pg',json_schema=json.loads(str(pgschema)),row_condition="a=='1' and b=='2' and c=='3'",condition_parser='pandas')
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
how can i create a proper json object to feed to this method?
There's no such thing as a "JSON object". JSON is a serialization format, so the only thing that can be JSON is a string of bytes. Such a string can be created with the very opposite of json.loads, namely json.dumps:
df_ge.expect_column_values_to_match_json_schema(column='pg',json_schema=json.dumps(pgschema),row_condition="a=='1' and b=='2' and c=='3'",condition_parser='pandas')
I'm trying to load a JSON file in to Python, but it's giving me an JSONDecodeError error which seems to suggest the file is empty... which I know is not true.
I've reviewed similar questions I can find on here (65466227 & 62286816), and while one or two provide useful alternatives... that's not really the point. I feel like what I'm doing should work but it doesn't.
I put my JSON in to jsonlint.com and it confirmed that my JSON is indeed valid.
Error message/traceback:
Traceback (most recent call last):
File "utils/fmt_test.py", line 62, in <module>
test_cases_json = json.load(jf)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/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.7/lib/python3.7/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)
My Python code:
test_case_file, _ = os.path.splitext(fmt_file)
test_case_file = f"{test_case_file}.json"
if os.path.isfile(test_case_file):
with open(test_case_file, "r") as jf:
test_cases_json = json.load(jf)
My JSON:
{
"tests": [
{
"id": "746fd83s23dy",
"event": "%t LFA_DUMMYSTRING LFA_DUMMYSTRING TEST LFA_DUMMYSTRING1 LFA_DUMMYSTRING2 TEST2 LFA_DUMMYSTRING",
"match": "True"
},
{
"id": "990gb98s34dm",
"event": "%t LFA_DUMMYSTRING LFA_DUMMYSTRING1 LFA_DUMMYSTRING2",
"match": "True"
},
{
"id": "100ma09x31ui",
"event": "%t localhost LFA_DUMMYSTRING1 LFA_DUMMYSTRING2 TEST3 LFA_DUMMYSTRING1 LFA_DUMMYSTRING2",
"match": "True"
}
]
}
Any help is much appreciated, thanks.
There is likely a UTF-8 BOM encoded at the beginning of the file since it is complaining about the first byte. Open with encoding='utf-8-sig' and it will be removed if present:
>>> import json
>>> data = {}
>>> with open('test.json','w',encoding='utf-8-sig') as f: # write data with BOM
... json.dump(data,f)
...
>>> with open('test.json') as f: # read it back
... data2 = json.load(f)
...
Traceback (most recent call last):
<traceback removed>
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) # same error
>>> with open('test.json',encoding='utf-8-sig') as f:
... data2 = json.load(f)
...
>>> # worked
My JSON file looks like this with filename as constants.json:
[
{
"PK": "abc",
"SK": "def",
"label": "eee",
"value": "fgh",
}
]
To read and print this in my python file, I used this code:
import json
with open('constants.json') as file1:
data=json.load(file1)
for item in data:
print(item)
I am getting an error when running the python file in visual studio code
File "<stdin>", line 2, in <module>
File "C:\Users\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\AppData\Local\Programs\Python\Python37\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)
>>> for item in data:
... print(item)
When you receive this kind of error:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Your first instinct should be to check if the JSON is valid, because that error means Python's json decoder couldn't parse your JSON file properly. One way to validate your JSON file is to post it on https://jsonlint.com/, and posting that constants.json would show up with this error:
Error: Parse error on line 5:
..., "value": "fgh",}]
--------------------^
Expecting 'STRING', got '}'
The error is a bit cryptic, but that site offers some hints at the bottom of the page:
Common Errors
Expecting 'STRING' - You probably have an extra comma at the end of your collection. Something like { "a": "b", }
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[' - You probably have an extra comma at the end of your list. Something
like: ["a", "b", ]
And as llamaking136 pointed out in their answer, the error is from the trailing comma after "fgh",.
Since you also mentioned Visual Studio Code, the IDE provides built-in linting of your JSON files, and if there are any errors, it will shows up with red squiggly lines and display a list of errors on the Problems tab:
So you can see and resolve the problem even before running any code.
You have an error in your JSON, line 6.
The dictionary ends with a comma, and the JSON interpreter expects another value after that, but it reaches the end before it can find it.
[
{
"PK": "abc",
"SK": "def",
"label": "eee",
"value": "fgh"
}
]
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)