JSON array to Map

208 views
Skip to first unread message

José Guilherme Vanz

unread,
Jan 9, 2015, 6:07:36 AM1/9/15
to play-fr...@googlegroups.com
Hi!

I am writing some reads to parse my JSON string. This json allows the user send any kind of additional data to save in the server. Example:

val json = """{ "field1": "blablabla",
                "
info": [
                         "
userField": 1,
                         "
userField2": "hi"
                        ]
               }"""


The field "info" is json array with all additional info sent by the user. The user can define the key and the value ( only number, string and boolean values ).
My question is: How can I convert this json array to map where the key is the key sent by the user and the corresponding value?
I have been trying to convert this using reads, but could not access the field name from the JsValue. Is possible do that using Reads?

PS: I'm using Play 2.2.x

Thank you

ibsdevs

unread,
Jan 9, 2015, 8:03:24 AM1/9/15
to play-fr...@googlegroups.com
Is your json object is a valid json? Your array has name value pairs, try to use an array of objects.

José Guilherme Vanz

unread,
Jan 9, 2015, 9:45:07 AM1/9/15
to play-fr...@googlegroups.com
Yeah. Actually my current version is using an array of objects. This way I can get the fields names but I would like to use just and fields with the values avoiding to create json object inside the array.
Is possible just using the Play framework API?

On Fri, Jan 9, 2015 at 11:03 AM, ibsdevs <ibs...@googlemail.com> wrote:
Is your json object is a valid json? Your array has name value pairs, try to use an array of objects.

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/VlBUQ2JPdcA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Att. José Guilherme Vanz
br.linkedin.com/pub/josé-guilherme-vanz/51/b27/58b/
"O sofrimento é passageiro, desistir é para sempre" - Bernardo Fonseca, recordista da Antarctic Ice Marathon.

Ryan Tanner

unread,
Jan 9, 2015, 2:21:53 PM1/9/15
to play-fr...@googlegroups.com
That isn't possible because it isn't valid JSON:

scala> val json = """{ "field1": "blablabla"

    |                 "info": [

    |                          "userField": 1,

    |                          "userField2": "hi"

    |                         ]

    |                }"""

json: String =

{ "field1": "blablabla",

               "info": [

                        "userField": 1,

                        "userField2": "hi"

                       ]

              }


scala> Json.parse(json)

com.fasterxml.jackson.core.JsonParseException: Unexpected character (':' (code 58)): was expecting comma to separate ARRAY entries

at [Source: java.io.StringReader@4c2a33ec; line: 3, column: 38]

 at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1524)

 at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:557)

 at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:475)

 at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:600)

 at play.api.libs.json.JsValueDeserializer.deserialize(JsValue.scala:408)

 at play.api.libs.json.JsValueDeserializer.deserialize(JsValue.scala:360)

 at play.api.libs.json.JsValueDeserializer.deserialize(JsValue.scala:355)

 at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2965)

 at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1587)

 at play.api.libs.json.JacksonJson$.parseJsValue(JsValue.scala:486)

 at play.api.libs.json.Json$.parse(Json.scala:19)

 ... 43 elided

Thomas Toye

unread,
Jan 10, 2015, 11:09:36 AM1/10/15
to play-fr...@googlegroups.com
As Ryan said, your JSON is not valid.


val json = """{ "field1": "blablabla",
                "
info": [
                         "
userField": 1,
                         "
userField2": "hi"
                        ]
               }"""


Your key "info" is a JSON array, not a JSON object. Objects map keys to a value (as in {"key": "value"} ), while arrays are simply lists of values.
You can try this:


val json = """{ "field1": "blablabla",
                "
info": {
                         "
userField": 1,
                         "
userField2": "hi"
                        }
               }"""

, where info is now an object. Or you could make the entries objects:

val json = """{ "field1": "blablabla",
                "
info": [
                         {"
userField
": 1},
                         {"
userField2": "hi"}
                        ]
               }"""


You can always try http://jsoneditoronline.org/ or a console with Jackon in the classpath to check your JSON.

José Guilherme Vanz

unread,
Jan 12, 2015, 9:49:55 AM1/12/15
to play-fr...@googlegroups.com
Yeah, you are right guys. Sorry about my silly fault. I was very distracted. I solved my problems.

Thank you for your patience

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/VlBUQ2JPdcA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages