I am trying to get access to stored values from local JSON files.
I load the JSON via
let json_url = "./static/" captureMode ".json";
let jsondata = {};
await fetch(json_url)
.then(response => response.json())
.then(data => jsondata = data);
But when trying to parse via let parsedData = JSON.parse(jsondata);
I get the error "Uncaught (in promise) SyntaxError: "[object Object]" is not valid JSON".
Edit:
Also directly trying to access values from jsondata
after response.json()
does result in an undefined output.
Does anybody have an idea what's happening?
EDIT:
The JSON looks like this:
{
"CameraSettings": {
"rawOnOff": true,
"shutter": 10000,
"autoWhiteBalance": false,
"exposureMode": "normal",
"exposure": 0,
"gains1": 1.6,
"gains2": 1.5,
"width": 4500,
"height": 3600,
"filename": "Before_Val_",
"counter": 0,
"sharpness": 1,
"contrast": 1,
"brightness": 50,
"saturation": 0,
"videoBitrate": 100,
"videoFPS": 16.666,
"videoLength": 1000,
"accuracy": 100000,
"framecount": 1,
"offset": 0,
"skipped_frames": 0
}
}
CodePudding user response:
That's because jsondata
is already the parsed object, as response.json()
will parse the responsed JSON text. You may just use jsondata
as parsedData
.
Besides, it's really strange and unclear to use await
and Promise.prototype.then()
at the same time. I think the code can be improved to:
let response = await fetch(json_url);
let parsedData = await response.json();