Processing Rest2Soap Response with JsonSlurper When Identity Data Service Returns Empty Response
Groovy Script
import groovy.json.JsonOutput
def parser = new groovy.json.JsonSlurper()
def jsonResp = parser.parseText(responseBodyTextToClient)
String adBilgisi = jsonResp.BilesikKutukBilgileri.TCVatandasiKisiKutukleri.KisiBilgisi.TemelBilgisi.Ad
String soyadBilgisi = jsonResp.BilesikKutukBilgileri.TCVatandasiKisiKutukleri.KisiBilgisi.TemelBilgisi.Soyad
if (adBilgisi == null || adBilgisi.isEmpty()) {
statusCodeToClient = 503
def pretty = JsonOutput.prettyPrint(JsonOutput.toJson([
ad: "",
soyad: ""
]))
responseBodyTextToClient = pretty
} else {
def pretty = JsonOutput.prettyPrint(JsonOutput.toJson([
ad: adBilgisi,
soyad: soyadBilgisi
]))
responseBodyTextToClient = pretty
}
Explanation
This script performs the following operations:
- Parsing JSON Response: Parses the JSON response coming from REST service using
JsonSlurper. - Data Extraction: Extracts name and surname information from JSON structure.
- Control and Conversion:
- If name information is empty or null, returns an empty JSON response with HTTP 503 status code.
- If name information exists, returns a JSON response containing only name and surname information.
Usage Scenario
This script processes the response from identity verification service:
- Returns error code in case of empty response
- Filters and returns only necessary fields in case of valid response
not
This script should be run on the response line (Response Policy) because it uses the responseBodyTextToClient variable.