Tom Medley Tom Medley -5 years ago 128
JSON Question

Get Double from JSON structure

I've updated a JSON library and I'm having trouble extracting the value I want.

The JSON looks like this:

"k1": {
"k2 : {
"k3" : 123

Previously, this did the trick:

(parse(json) \\ "k1" \\ "k2" \\ "k3").extract[Double]

did the trick, now it does not. Now I get the exception:

org.json4s.package$MappingException: Do not know how to convert JArray(List(JInt(123))) into double

I tried using
compact(render(parse(json) \\ "k1" \\ "k2" \\ "k3"))
, but that gave me the String
, which is not really what I want. How can I extract the value directly as a double?

Answer Source

Using \\ accessor is incorrect here, as it walks the JSON recursively and extracts any "k1" it encounters, producing a JArray, thus the error.

You should either use \ which accesses only one directly nested key of the JSON object, or extract[List[Double]] from your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download