Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
@robertmryan

This comment has been minimized.

Copy link
Owner Author

commented Aug 28, 2019

The result of the above, as you’d expect, is:

transactionId: f7c4d630-552b-46d9-a37d-44450537b48d

response: {"Response":{"transactionId":"f7c4d630-552b-46d9-a37d-44450537b48d","content":{"Reset":{}}}}

@robertmryan

This comment has been minimized.

Copy link
Owner Author

commented Aug 28, 2019

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
You can’t perform that action at this time.