1

Below is the JSON, in which I need to parse the value of:

"primaryInvolvedParty": 
{
}

The response is XML, hence it is converted into JSON, so that I can parse it easily.

Converting XML to JSON using:

String messageBody = actualResult.get("StatusMessage");
JSONObject xmlconvertedjson=XML.toJSONObject(messageBody);
JsonElement jelement = new 
JsonParser().parse(xmlconvertedjson.toString());

But I am looking for the generic function where I can fetch the key-value for given attribute, for ex: customerScore.scoreResult= Accept, Please help as I am new to Rest Assured parsing.

JSON:

{
  "soap:Envelope": {
    "soap:Header": "",
    "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
    "soap:Body": {
      "ns2:offerProductArrangementResponse": {
        "xmlns:ns2": "http://LIB_SIM_SalesProcessManagement/IA_OfferProductArrangement",
        "response": {
          "productArrangement": {
            "arrangementId": "317076",
            "offeredProducts": {
              "externalSystemProductIdentifier": [
                {
                  "productIdentifier": "901",
                  "systemCode": "00107"
                },
                {
                  "productIdentifier": "201",
                  "systemCode": "10107"
                },
                {
                  "productIdentifier": "P_CLASSIC",
                  "systemCode": "00010"
                },
                {
                  "productIdentifier": "0071306000",
                  "systemCode": "00004"
                },
                {
                  "productIdentifier": "0071776000",
                  "systemCode": "00004"
                }
              ],
              "productIdentifier": "92",
              "productoffer": {
                "offerType": "2001"
              },
              "associatedFamily": {
                "extsysprodfamilyidentifier": {
                  "productFamilyIdentifier": "502"
                }
              },
              "productoptions": [
                {
                  "optionsDescription": "Correspondence Default",
                  "optionsName": "Correspondence Default",
                  "optionsType": "CorrespDef",
                  "optionsValue": "Paper",
                  "optionsCode": "3521"
                },
                {
                  "optionsDescription": "Correspondence Option",
                  "optionsName": "Correspondence Option",
                  "optionsType": "CorrespOp",
                  "optionsValue": "Choice",
                  "optionsCode": "2944"
                },
                {
                  "optionsDescription": "Debit Card Funding",
                  "optionsName": "Debit Card Funding",
                  "optionsType": "DCF",
                  "optionsValue": "N",
                  "optionsCode": "3943"
                },
                {
                  "optionsDescription": "Product Priority",
                  "optionsName": "Product priority",
                  "optionsType": "PrdPriority",
                  "optionsValue": "5",
                  "optionsCode": "2658"
                },
                {
                  "optionsDescription": "Upsell Display Value",
                  "optionsName": "Upsell Display Value",
                  "optionsType": "UPSellDispval",
                  "optionsValue": "0",
                  "optionsCode": "2659"
                },
                {
                  "optionsDescription": "Account Switching",
                  "optionsName": "Account Switching",
                  "optionsType": "AccSwitching",
                  "optionsValue": "Y",
                  "optionsCode": "2662"
                },
                {
                  "optionsDescription": "VERDE PRODUCT ID",
                  "optionsName": "VERDE PRODUCT ID",
                  "optionsType": "VerdeProdId",
                  "optionsValue": "20051",
                  "optionsCode": "4523"
                },
                {
                  "optionsDescription": "Vantage Product Id for Verde",
                  "optionsName": "Vantage Product Id for Verde",
                  "optionsType": "VantProdIDVer",
                  "optionsValue": "20055",
                  "optionsCode": "4963"
                },
                {
                  "optionsDescription": "isVantage",
                  "optionsName": "isVantage",
                  "optionsType": "isVantage",
                  "optionsValue": "No",
                  "optionsCode": "6659"
                },
                {
                  "optionsDescription": "Overdraft Offer Flag",
                  "optionsName": "Overdraft Offer Flag",
                  "optionsType": "OD_Offer_Flag",
                  "optionsValue": "Y",
                  "optionsCode": "6905"
                },
                {
                  "optionsDescription": "Make_It_Joint",
                  "optionsName": "Make_It_Joint",
                  "optionsType": "MkItJoint",
                  "optionsValue": "1",
                  "optionsCode": "6975"
                },
                {
                  "optionsDescription": "Survey",
                  "optionsName": "Survey",
                  "optionsType": "Survey",
                  "optionsValue": "Y",
                  "optionsCode": "7288"
                },
                {
                  "optionsDescription": "Statement Option",
                  "optionsName": "Statement Option",
                  "optionsType": "StateOp",
                  "optionsValue": "Choice",
                  "optionsCode": "1099"
                },
                {
                  "optionsDescription": "Statement Default",
                  "optionsName": "Statement Default",
                  "optionsType": "StateDef",
                  "optionsValue": "Paper",
                  "optionsCode": "1770"
                },
                {
                  "optionsDescription": "Tariff",
                  "optionsName": "Tariff",
                  "optionsType": "TRF",
                  "optionsValue": "1",
                  "optionsCode": "2663"
                }
              ],
              "productName": "Classic Account"
            },
            "applicationType": "10001",
            "xsi:type": "ns7:DepositArrangement",
            "isOverdraftRequired": "true",
            "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "xmlns:ns7": "http://LIB_SIM_BO/BusinessObjects",
            "primaryInvolvedParty": {
              "auditData": [
                {
                  "auditTime": "132551",
                  "auditType": "PARTY_EVIDENCE",
                  "auditDate": "12012018"
                },
                {
                  "auditTime": "132551",
                  "auditType": "ADDRESS_EVIDENCE",
                  "auditDate": "12012018"
                },
                {
                  "auditTime": "132551",
                  "auditType": "ADDRESS",
                  "auditDate": "12012018"
                }
              ],
              "isAuthCustomer": "false",
              "cidPersID": "+00554133625",
              "customerSegment": "3",
              "individualIdentifier": "249894",
              "newCustomerIndicator": "true",
              "customerIdentifier": "2012794088",
              "customerScore": [
                {
                  "scoreResult": "ACCEPT",
                  "assessmentType": "EIDV",
                  "decisionText": "001",
                  "decisionCode": "I",
                  "assessmentEvidence": {
                    "addressStrength": "900",
                    "evidenceIdentifier": "4696198HL890678339009003211586646102",
                    "identityStrength": "900"
                  }
                },
                {
                  "scoreResult": "1",
                  "assessmentType": "ASM",
                  "referralCode": [
                    {
                      "code": "601",
                      "description": "Accept."
                    },
                    {
                      "code": "601",
                      "description": "Accept."
                    }
                  ],
                  "scoreIdentifier": "647448142"
                }
              ]
            },
            "arrangementType": "CA",
            "applicationStatus": "1002",
            "associatedProduct": {},
            "conditions": [
              {
                "result": "No",
                "name": "INTEND_TO_SWITCH"
              },
              {
                "result": "true",
                "name": "INTEND_TO_OVERDRAFT"
              },
              {
                "result": "Y",
                "name": "CHECK_BOOK_OFFERED_FLAG"
              },
              {
                "result": "20",
                "name": "DEBIT_CARD_RISK_CODE"
              },
              {
                "result": "4",
                "name": "OVERDRAFT_RISK_CODE"
              }
            ],
            "overdraftDetails": {
              "amount": {
                "amount": "3001"
              }
            }
          },
          "header": {
            "interactionId": "ucoGI58OmuY0bfYXx2iLqta",
            "LloydsHeaders": [
              {
                "prefix": "ns4",
                "name": "ServiceRequest",
                "nameSpace": "http://www.lloydstsb.com/Schema/Infrastructure/SOAP",
                "value": {
                  "ns5:From": "10.245.211.214",
                  "xsi:type": "ns5:ServiceRequest",
                  "ns5:Action": "offerProductArrangement",
                  "ns5:ServiceName": "offerProductArrangement",
                  "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
                  "ns5:mustReturn": "false",
                  "ns5:MessageId": "ucoGI58OmuY0bfYXx2iLqta"
                }
              },
              {
                "prefix": "ns4",
                "name": "ContactPoint",
                "nameSpace": "http://www.lloydstsb.com/Schema/Infrastructure/SOAP",
                "value": {
                  "ns5:OperatorType": "Customer",
                  "ns5:ContactPointId": "0000777505",
                  "ns5:ApplicationId": "AL07618",
                  "ns5:InitialOriginatorId": "10.245.224.125",
                  "xsi:type": "ns5:ContactPoint",
                  "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
                  "ns5:mustReturn": "false",
                  "ns5:InitialOriginatorType": "Browser",
                  "ns5:ContactPointType": "003"
                }
              },
              {
                "prefix": "ns5",
                "name": "bapiInformation",
                "nameSpace": "http://www.lloydstsb.com/Schema/Enterprise/LCSM",
                "value": {
                  "ns4:BAPIId": "B001",
                  "xsi:type": "ns4:bapiInformation",
                  "ns4:BAPIOperationalVariables": {
                    "ns4:bForceHostCall": "false",
                    "ns4:bBatchRetry": "false",
                    "ns4:bPopulateCache": "false"
                  },
                  "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
                  "ns4:BAPIHeader": {
                    "ns4:acceptLanguage": {
                      "xsi:nil": "true"
                    },
                    "ns4:inboxidClient": "GX",
                    "ns4:cctmauthcd": {
                      "xsi:nil": "true"
                    },
                    "ns4:encVerNo": {
                      "xsi:nil": "true"
                    },
                    "ns4:sessionid": "ucoGI58OmuY0bfYXx2iLqta",
                    "ns4:useridAuthor": "UNAUTHSALE",
                    "ns4:userAgent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
                    "ns4:chanidObo": {
                      "xsi:nil": "true"
                    },
                    "ns4:stpartyObo": {
                      "ns4:ocisid": "737704834",
                      "ns4:partyid": "+00263174828",
                      "ns4:host": "I"
                    },
                    "ns4:chanid": "IBL",
                    "ns4:ipAddressCaller": {
                      "xsi:nil": "true"
                    },
                    "ns4:chanctxt": "1",
                    "ns4:callerlineid": {
                      "xsi:nil": "true"
                    },
                    "ns4:chansecmode": "PWD"
                  }
                }
              },
              {
                "prefix": "lgsm",
                "name": "Security",
                "nameSpace": "http://LB_GBO_Sales/Messages",
                "value": {
                  "mustReturn": "false",
                  "ns3:UsernameToken": {
                    "ns3:Username": "UNAUTHSALE",
                    "ns3:UNPMechanismType": "NTLM",
                    "Id": "LloydsTSBSecurityToken",
                    "ns3:UserType": "013"
                  },
                  "xsi:type": "ns3:SecurityHeaderType",
                  "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
                }
              }
            ],
            "businessTransaction": "OfferProductArrangement",
            "channelId": "LTB"
          }
        },
        "xmlns:ns5": "http://www.lloydstsb.com/Schema/Infrastructure/SOAP",
        "xmlns:ns3": "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
        "xmlns:ns4": "http://www.lloydstsb.com/Schema/Enterprise/LCSM"
      }
    }
  }
}
Tristan
  • 279
  • 5
  • 21
Nithin
  • 11
  • 3
  • what is this `enter code here` supposed to mean in your json string ?? – Nithin Jan 12 '18 at 14:41
  • I think you had intended to put the XML in the question. This would be helpful to get a more clear picture of the full lifecycle. Also is there a reason that you can't get the information directly from the XML? – Tristan Jan 12 '18 at 14:44
  • Yes, since my response had soap(soap:Envelope), i wanted to clearly mention that i used Xml converter to convert this to Json. Also i was not comfortable with xml, so was trying to parse Json.Hence converted to Json, and was trying to parse using java – Nithin Jan 12 '18 at 14:59
  • Please ignore enter code here, sorry typo error – Nithin Jan 12 '18 at 15:01
  • Please remember to mark an answer if one worked for you – Tristan Jan 13 '18 at 22:34

3 Answers3

0

I think you can build your function using XPath or JsonPath as base of your solution

pedrohreis
  • 1,030
  • 2
  • 14
  • 33
0

The specified json response is nested. Through method chaining technique the code can be written as follows:

String messageBody = actualResult.get("StatusMessage");
JSONObject xmlconvertedjson=XML.toJSONObject(messageBody);
JsonElement primaryInvolvedParty = xmlconvertedjson.get("soap:Envelope").getAsJsonObject().get("soap:Body").getAsJsonObject().get("ns2:offerProductArrangementResponse").getAsJsonObject().get("response").getAsJsonObject().get("productArrangement").getAsJsonObject().get("primaryInvolvedParty");

For readability this code can be written as follows:

String messageBody = actualResult.get("StatusMessage");
JSONObject xmlconvertedjson = XML.toJSONObject(messageBody);
JsonElement soapEnvelope = xmlconvertedjson.getAsJsonObject().get("soap:Envelope");
JsonElement soapBody = soapEnvelope.getAsJsonObject().get("soap:Body");
JsonElement ns2offerProductArrangementResponse = soapBody.getAsJsonObject().get("ns2:offerProductArrangementResponse");
JsonElement response = ns2offerProductArrangementResponse.getAsJsonObject().get("response");
JsonElement productArrangement = response.getAsJsonObject().get("productArrangement");
JsonElement primaryInvolvedParty = productArrangement.getAsJsonObject().get("primaryInvolvedParty");
Nithin
  • 748
  • 1
  • 10
  • 27
  • Thank you very much for the code.Is there any way ,where we can write generic method to handle the nested JSON like this ? – Nithin Jan 15 '18 at 10:35
  • According to your example- `customerScore.scoreResult= Accept` , it is possible to obtain a list of `customerScore` objects. What kind of generic method are u expecting on this list ?? – Nithin Jan 15 '18 at 18:25
0

@Nithin Dhanyavadagalu Thanks for the solution.Below code worked , but looking for some generic code.

String messageBody = actualResult.get("StatusMessage");
         JSONObject xmlconvertedjson=XML.toJSONObject(messageBody);
         JsonElement jelement = new JsonParser().parse(xmlconvertedjson.toString());
         JsonObject jobject = jelement.getAsJsonObject();

         System.out.println("Parsed json object"+jobject);

         JsonElement soapEnvelope = jobject.get("soap:Envelope").getAsJsonObject().get("soap:Body").getAsJsonObject().get("ns2:offerProductArrangementResponse").getAsJsonObject().get("response").getAsJsonObject().get("productArrangement");
         JsonElement primaryInvolvedParty = soapEnvelope.getAsJsonObject().get("primaryInvolvedParty");
         JsonElement audit = primaryInvolvedParty.getAsJsonObject().get("customerScore");

         System.out.println("Primaryparty:"+audit);

         JsonArray jsonarray = audit.getAsJsonArray();
         System.out.println( jsonarray.get(0).getAsJsonObject().get("scoreResult"));
         System.out.println( jsonarray.get(0).getAsJsonObject().get("assessmentType"));
         System.out.println( jsonarray.get(1).getAsJsonObject().get("assessmentType"));
Nithin
  • 11
  • 3