How do you modify the contents of a JSON object, and write it back out?

85 views
Skip to first unread message

Henry Minsky

unread,
Jun 13, 2011, 4:35:26 PM6/13/11
to Lift
Hello lift-experts!

I am trying to use the lift json library, and am confused about a very
simple task.

I have some JSON which contains an object, one of whose fields is a
list of strings:


scala> val j = parse("""{ "a": ["123", "1bfc", "ocac"], "b": 17}""")
j: net.liftweb.json.package.JValue =
JObject(List(JField(a,JArray(List(JString(123), JString(1bfc),
JString(ocac)))), JField(b,JInt(17))))

If I 'extract' the "a" field, I can get a List[String]

scala> (j \ "a").extract[List[String]]
res15: List[String] = List(123, 1bfc, ocac)

My question is, if I add or remove something from the list, how can I
stuff it back into the JSON object? I.e., I want to treat the field
"a" like a key in a Map, and update the value using something like a
Map.updated(key, newvalue) call?

Example code snippets appreciated!

Thanks!


Joni Freeman

unread,
Jun 14, 2011, 2:33:33 AM6/14/11
to lif...@googlegroups.com
Hi,

You can't modify a JValue since it is immutable. Transform function can
be used to make transformation on JValues. Example:

j transform {
case JField("a",JArray(xs)) => JField("a",JArray(JString("x")::xs))
}

Removal is done by transforming a value into JNothing:

j transform {
case JField("a", JArray(xs)) => JNothing
}

Two JSON:s can be merged with 'merge' function:

scala> val j1 = ("foo" -> 1) ~ ("bar" -> 2)
scala> val j2 = ("foo" -> 1) ~ ("baz" -> 3)
scala> j1 merge j2
res6: net.liftweb.json.package.JValue =
JObject(List(JField(foo,JInt(1)), JField(bar,JInt(2)),
JField(baz,JInt(3))))

Those are perhaps the most common ways to make transformations on the
JSON. 'replace' and 'remove' are helpers which can be used too.

Cheers Joni

Henry Minsky

unread,
Jun 14, 2011, 9:45:44 AM6/14/11
to lif...@googlegroups.com
Thanks, that's very helpful!

Can you tell me where to find the documentation on the json library, I cannot seem to locate
it on Google?

The only doc pointer I found was broken, on this page

http://www.scala-lang.org/node/4225

which points to

Currently the docs live here:
http://github.com/dpp/liftweb/tree/master/lift-base/lift-json/

Thanks!




--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Henry Minsky


Tim Nelson

unread,
Jun 14, 2011, 10:22:12 AM6/14/11
to lif...@googlegroups.com

Antonio Salazar Cardozo

unread,
Jun 14, 2011, 10:52:33 AM6/14/11
to lif...@googlegroups.com
Prepare to have your mind blown by the awesomeness detailed in the docs ;)
To unsubscribe from this group, send email to liftwe...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Henry Minsky


Reply all
Reply to author
Forward
0 new messages