Rename a json property…

46 views
Skip to first unread message

Viktor Hedefalk

unread,
May 31, 2015, 5:36:51 AM5/31/15
to argona...@googlegroups.com
Hi,

I'm sorry to repost. Tried to use the irc channel, but newbie me shut it down and I have no idea if there's already an answer there. Can't seem to find a way to check history. I'm not only argonaut newb, but irc newb too :)

Here's the question again:

Hi,  First I wanna say thanks! I’ve been using Argonaut with codecs to and from scala objects and really love the API. Currently I have this use case where I go directly from Mongo to Argonaut and then I need to move things around just a tiny bit before outputting json to a web client. So I’m trying to understand the cursor api.

My first issue is trying to rename an ”_id” field to just ”id”. I figured I could delete and add but can’t get my head around it. To get the value it seems I need to drill down with something like id.withFocus(_.withString(theIdValue => ???)) but I can only keep the value inside that function which needs to return a new value.

Almost felt tempted to use a ”var” for the first time ever :) Hehe, not really, but I really feel thick as a brick here. Any pointers highly appreciated!

Cheers,
Viktor

seantparsons

unread,
May 31, 2015, 7:43:29 AM5/31/15
to argona...@googlegroups.com, hede...@gmail.com
I would suggest leaving the cursor API to one side, you're better off using methods on the Json type to manipulate the structure like withObject.

Sean.

Viktor Hedefalk

unread,
Jun 1, 2015, 3:58:07 AM6/1/15
to argona...@googlegroups.com, hede...@gmail.com
Thanks Sean!

Yeah, I found http://stackoverflow.com/a/21267006/312873 and ended up with something as simple as:

def renameField(before: JsonField, after: JsonField)(obj: JsonObject) =
obj(before).map(v => (obj - before) + (after, v)).getOrElse(obj)

val updated = json.withObject(renameField("_id", "id"))

Cheers,
Viktor
Reply all
Reply to author
Forward
0 new messages