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
}
}
response.json(), the promise it returns is fulfilled when the body text has been read and parsed. So you don't want to doJSON.parse(jsondata);jsondata(which will want renaming) is already parsed, it's already an object. So passing it intoJSON.parse(which requires a string) does the implicit conversion to string, which gives you the string"[object Object]".fetchAPI -- it only rejects its promise on network errors, not HTTP errors). I have a writeup about it on my anemic old blog here.