Last active
August 29, 2015 13:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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