Last active
August 28, 2019 22:17
-
-
Save robertmryan/3f181810c17bba9eb982073f4983d815 to your computer and use it in GitHub Desktop.
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
func parse(message: String) -> String { | |
let json = JSON(parseJSON: message) | |
return json["Request"]["transactionId"].stringValue | |
} | |
let input = """ | |
{"Request": {"content": {"Reset": {}}, "transactionId": "f7c4d630-552b-46d9-a37d-44450537b48d"}} | |
""" | |
let transactionID = parse(message: input) | |
print("transactionId:", transactionID) | |
let response = String(format: "{\"Response\":{\"transactionId\":\"%@\",\"content\":{\"Reset\":{}}}}", transactionID) | |
print("response:", response) |
If it’s failing for you, you might want to add some error checking, e.g.:
func parse(message: String) -> String {
let json = JSON(parseJSON: message)
let request = json["Request"]
if let error = request.error {
fatalError("request error: \(error)")
}
let transactionId = request["transactionId"]
if let error = transactionId.error {
fatalError("transactionId error: \(error)")
}
return transactionId.stringValue
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The result of the above, as you’d expect, is: