Reputation: 809
I need to find an Id based on it's associated name. My program makes REST calls to an API. The API returns results in JSON format. The Name is unique, so I would like to use it to get it's Id value. Note the ... can contain anything and does include some {Such}Id keys. The Id's can be nested in any number of {...{...{...}...}...} The Id's are always immediately before the name.
Note: ... is code that for privacy reasons cannot be shown. The code itself(before exclusion of private data) is the result of a REST call as returned by Advanced Rest Client and verified at http://jsonlint.com/ to be valid JSON.
The code is returned as such:
{
Id: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
Name: "Automated Runs"
OrderNumber: 0
Expands: [3]
0: "Children"
1: "Parent"
2: "Project"
...
scripts: [4]
0: {
Id: "0b70a55c-5e68-4b27-bfcf-a22f00c5dc48"
Name: "3816"
PackageId: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
...
Expands: [6]
0: "Assignments"
1: "Attachments"
2: "FieldControls"
3: "FieldValues"
4: "Package"
5: "Steps"
...
1: {
Id: "14e5c663-0d5a-46bb-ac48-a22f00c15998"
Name: "3814"
PackageId: "d5a94d1a-afb7-4e1d-ae0d-a22e01393666"
ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
...
Expands: [6]
0: "Assignments"
1: "Attachments"
2: "FieldControls"
3: "FieldValues"
4: "Package"
5: "Steps"
...
2: {
Id: "00d52fcd-b611-4f69-aeb6-a22f00c263a9"
Name: "3815"
ProjectId: "ed61c45a-f208-4115-8584-a21a00c51ac0"
...
Expands: [6]
0: "Assignments"
1: "Attachments"
2: "FieldControls"
3: "FieldValues"
4: "Package"
5: "Steps"
...
3: {
Id: "4d3a6132-8497-4b6b-a064-a22f00c669ff"
Name: "3817"
...
Expands: [6]
0: "Assignments"
1: "Attachments"
2: "FieldControls"
3: "FieldValues"
4: "Package"
5: "Steps"
...
}
Things I have tried include regex (I'm new to it and having some troubles), and simple string splitting. While I have the string splitting working, it is semi hard coded.
What i would like is something like:
def getID(myJSON:String, myName:String){
val pattern = "\"Id\": \"*\",\r\n\"Name\":\"" + myName + "\",\""
get the id (*) from result using pattern
}
Or Even better convert it to be generic.
def getID(myJSON:String, myValue:String, searchKey:String, findKey:String){
val pattern = { ... findKey: *...} in the inner most { ... searchKey: * ...} scope
get the id (*) from result using the pattern in the found {...searchKey...} scope
}
Either would be great and very much appreciated. My current code looks like:
result.split("Id\": \"")(3).split("\"")(0)
It might be pretty, but it has lots of room for mishaps. An Id might be created by a user which sets the count to be incorrect, etc...
Thank you, Erick Stone
Upvotes: 1
Views: 251
Reputation: 14420
How about using the for comprehension in json4s
scala> :paste
// Entering paste mode (ctrl-D to finish)
import org.json4s._
import org.json4s.native.JsonMethods._
val json = """
{
"a": {
"Id": "1",
"Name": "Name1",
"b": {
"Id": "2",
"Name": "Name2",
}
}
}
"""
def getId(json: String, name: String) = {
val res = for {
JObject(child) <- parse(json)
JField("Name", JString(n)) <- child
JField("Id", JString(id)) <- child
if n == name
} yield id
res.headOption
}
// Exiting paste mode, now interpreting.
scala> getId(json, "Name1")
res4: Option[String] = Some(1)
scala> getId(json, "Name2")
res5: Option[String] = Some(2)
Upvotes: 1