webline
(Detlef Beyer)
August 20, 2023, 4:13pm
1
I got this JSON returned from an API:
{"app_version":"v1.1","time_taken":0.002452373504638672,"msg":"language detection successful","ok":true,"language_probability":{"en":"0.99999745365333"}}
now using the parser:
var:string probability = theResult.json["language_probability"];
returns { en:0.99999745365333 }
the quotes are missing, no JSON anymore.
so probability.json[0]
returns “no such field”.
var:string probability = theResult.jsonValue["language_probability"];
returns “no such field”. But the path to the field seems correct to me?!
Also var:string probability = theResult.jsonValue["language_probability"][0]
returns “no such field”.
For sure I’m doing something wrong?!
mwra
(Mark Anderson)
August 20, 2023, 7:09pm
2
This one I can see the error as .jsonValue()
uses parentheses so the syntax should be:
theResult.jsonValue("language_probability")
It seems to me that action code lists parsing of []
and dictionaries {}
may be confusing JSON parsing. But, I don’t know as I don’t consider myself expert with JavaScript and I’ve only the examples taken from release notes to to work with. So, I’m afraid I can’t really help.
Stream parsing needs some detailed worked examples using ll stream parsing commands, with a separate demo for each of text, JSON and XML streams.
webline
(Detlef Beyer)
August 20, 2023, 8:33pm
3
theResult.jsonValue("language_probability")
returns “”
mwra
(Mark Anderson)
August 20, 2023, 10:34pm
4
Indeed, but I’ve no idea why.
eastgate
(eastgate)
August 21, 2023, 6:00pm
5
webline:
{“app_version”:“v1.1”,
“time_taken”:0.002452373504638672,
“msg”:“language detection successful”,
“ok”:true,
“language_probability”:{“en”:“0.99999745365333”}
}
The value of “language probability” is a dictionary with one key, “en”. So:
var:string number = theResult.json["language_probability"]["en"];
➛ 0.999974...
This is now in the test suite. We could also use
theResult.json.jsonValue('language_probability.en');
jsonValue()
extracts a value from the current json object, which is captured by .json
.
I would now recommend the first approach; jsonValue()
help route around problems with representing nested objects, but I believe those are behind us.