Skip to content

Instantly share code, notes, and snippets.

@mikesname
Last active August 29, 2015 13:56
Show Gist options
  • Save mikesname/9027468 to your computer and use it in GitHub Desktop.
Save mikesname/9027468 to your computer and use it in GitHub Desktop.
Stackoverflow: How to replace all the values with the same key in a JSON tree
// Reply to: http://stackoverflow.com/questions/21801071/how-to-replace-all-the-values-with-the-same-key-in-a-json-tree/21804784#comment32996624_21804784
/**
* If the object matches an $oid, return the value and the new key
* Otherwise, return the original value
*/
def idOrValue(key: String, js: JsValue): (String, JsValue) = js \ "$oid" match {
case JsUndefined() => key -> js
case v => key -> v
}
def toPublic(json: JsValue) = json.updateAllKeyNodes {
case ((_ \ "_id"), value) => idOrValue("id", value)
case ((_ \ "accountId"), value) => idOrValue("accountId", value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment