Forum Discussion

rakesh133's avatar
rakesh133
Occasional Contributor
7 years ago

JsonSlurplerGetting Error while Parsing the Json Response

I tried to create a groovy script to parse the JSON response to a variable using JsonSlurper. Though the response Json is in valid format, i am getting an error

 

def responseContent = testRunner.testCase.testSteps["Neme fof the test step"].testRequest.response.contentAsString
def Response = new JsonSlurper().parseText(responseContent)
log.info Code

 

Could some one please look into this issue provide me a workaround for this. Thanks in advance

  • rakesh133's avatar
    rakesh133
    Occasional Contributor

    nmrao The Json response array is something like below. 

     

    [
        {
            "AgreementType": "XXXX",
            "Code": "XXXX",
            "ShortName": "XXXX",
            "Name": "XXXX",
            "Description": "XXXX",
            "DocumentManagementStoreUrl": null,
            "IsActive": true,
            "EffectiveDate": "2013-12-10T00:00:00",
            "TerminationDate": null,
            "WasCreatedInError": false,
            "DataTimeStamp": "2018-05-04T13:34:01Z"
        },
        {
            "AgreementType": "XXXX",
            "Code": "XXXX",
            "ShortName": "XXXX",
            "Name": "XXXX",
            "Description": "XXXX",
            "DocumentManagementStoreUrl": null,
            "IsActive": true,
            "EffectiveDate": "2013-12-10T00:00:00",
            "TerminationDate": null,
            "WasCreatedInError": false,
            "DataTimeStamp": "2018-05-04T13:34:01Z"
        },
        {
            "AgreementType": "XXXX",
            "Code": "XXXX",
            "ShortName": "XXXX",
            "Name": "XXXX",
            "Description": "XXXX",
            "DocumentManagementStoreUrl": null,
            "IsActive": true,
            "EffectiveDate": "2013-12-10T00:00:00",
            "TerminationDate": null,
            "WasCreatedInError": false,
            "DataTimeStamp": "2018-05-04T13:34:01Z"
        },
        {
            "AgreementType": "XXXX",
            "Code": "XXXX",
            "ShortName": "XXXX",
            "Name": "XXXX",
            "Description": "XXXX",
            "DocumentManagementStoreUrl": null,
            "IsActive": true,
            "EffectiveDate": "2013-12-10T00:00:00",
            "TerminationDate": null,
            "WasCreatedInError": false,
            "DataTimeStamp": "2018-05-04T13:34:01Z"
        }
  • rakesh133's avatar
    rakesh133
    Occasional Contributor

    nmraoJHunt Thanks for the update. I am now able to get the response json in UTF-8 format.

     

    Now the Json array count is showing 0 eventhough I have many array elements. Please see below the output.

    Please could you get a fix for this issue as well. 


  • JHunt's avatar
    JHunt
    Community Hero

    Go to your request, look for the Headers tab down the bottom. Add:

     

    Accept = application/json;charset=UTF-8

    and if that doesn't fix it, try adding

     

    Accept-Charset = UTF-8

    Lastly (but I don't think this is the issue) you could try adding:

     

    Accept-Encoding = identity
  • JHunt's avatar
    JHunt
    Community Hero

    The error in the first post (referring to character 65279) is an encoding problem - there is a BOM in your response.You may need to add or amend your Accept/Encoding header on your request.

     

    Once you solve your encoding problem, it should work (you don't need that JsonOutput.toJson stuff).

     

    This assumes your actual response JSON is valid. The JSON you posted is NOT valid, because the special characters aren't escaped. Only \ and " need to be escaped as \\ and \"

     

        	"Name": "Long Name (Including Special Characters like ',-,\\,\" etc)"

    But from what I understand, I think you've edited that and that's not your real response. Since you say your real response validates OK, your real response must be properly escaped or doesn't contain those characters (check the raw view in SoapUI to see if your actual JSON response is escaped, rather than the JSON view.)

     

     

    • rakesh133's avatar
      rakesh133
      Occasional Contributor

      JHuntThanks for your update. Could you please tell me how to add or amend the 'Accept/Encoding header' on the request as I am not an expert in this.

       

      Also when I checked the Raw format of the file I could see some special character in the response which is not present in the JSON format response

       

      Please note I am always selecting the encoding as 'UTF-8' in the properties. Please see below.

       This is the encoding type I have selected.

       

       

       

  • rakesh133's avatar
    rakesh133
    Occasional Contributor

    Hi All,

     

    Finally, I found a workaround for this issue. I passed the Json response to "def json = JsonOutput.toJson(response)" based on this stackoverflow question. Now the response is successfully parsed evenif the special characters are present

     

    The Groovy code will be something like this.

    import groovy.json.*
    def responseContent = testRunner.testCase.getTestStepByName("Name of the Test Step").getPropertyValue("response")
    def json = JsonOutput.toJson(responseContent)
    def result = new JsonSlurper().parseText(json)
    log.info result

    Lets see how it goes and I will update you.

     

  • rakesh133's avatar
    rakesh133
    Occasional Contributor

    Apologies for not adding the JSON Response. Please see below the JSON Response array I am receiving for the service

     

    [
        {
            "Type": "XXXX",
            "Code": "XXXXX",
            "ShortName": "ShortName",
            "Name": "Long Name (Including Special Characters like ',-,\," etc)",
            "Description": "Description of the Service Agreement (Including Special Characters like ',-,\," etc)"",
            "DocumentManagementStoreUrl": null,
            "IsActive": true,
            "EffectiveDate": "2013-12-10T00:00:00",
            "TerminationDate": null,
            "WasCreatedInError": false,
            "DataTimeStamp": "2018-04-30T08:42:45Z"
        },
        {
            "Type": "XXXX",
            "Code": "XXXXX",
            "ShortName": "ShortName",
            "Name": "Long Name (Including Special Characters like ',-,\," etc)",
            "Description": "Description of the Service Agreement (Including Special Characters like ',-,\," etc)"",
            "DocumentManagementStoreUrl": null,
            "IsActive": true,
            "EffectiveDate": "2013-12-10T00:00:00",
            "TerminationDate": null,
            "WasCreatedInError": false,
            "DataTimeStamp": "2018-04-30T08:42:45Z"
        },
        {
         .....
        },
        {
        .....
        },
    	
    ]
    • nmrao's avatar
      nmrao
      Champion Level 3
      rakesh133, If the json which you are receiving is valid, then you are not supposed to get the mentioned error(at least with the first 2 statements of code snippet which you have shown).
      • rakesh133's avatar
        rakesh133
        Occasional Contributor

        nmrao The Json response I have is a valid one as I confirmed it in the Json validator. Please see below the details.

         

         

         

         

    • avidCoder's avatar
      avidCoder
      Super Contributor

      Hey, Would you please try below code:-

       

      import groovy.json.JsonSlurper
      
      def json = '''[
          {
              "Type": "XXXX",
              "Code": "XXXXX",
              "ShortName": "ShortName",
              "Name": "",
              "Description": "",
              "DocumentManagementStoreUrl": null,
              "IsActive": true,
              "EffectiveDate": "2013-12-10T00:00:00",
              "TerminationDate": null,
              "WasCreatedInError": false,
              "DataTimeStamp": "2018-04-30T08:42:45Z"
          },
          {
              "Type": "XXXX",
              "Code": "XXXXX",
              "ShortName": "ShortName",
              "Name": "",
              "Description": "",
              "DocumentManagementStoreUrl": null,
              "IsActive": true,
              "EffectiveDate": "2013-12-10T00:00:00",
              "TerminationDate": null,
              "WasCreatedInError": false,
              "DataTimeStamp": "2018-04-30T08:42:45Z"
          }
          ]'''
      
          def code = new JsonSlurper().parseText(json)
          log.info code

      you have used some unexpected character "/" because of which you might be getting errors. I have removed these characters from "Name" and "Description" key. 

       

      Here is how you will get the output of the above code:-

       

      Mon Apr 30 18:35:23 IST 2018:INFO:[{Code=XXXXX, DataTimeStamp=2018-04-30T08:42:45Z, Description=, DocumentManagementStoreUrl=null, EffectiveDate=2013-12-10T00:00:00, IsActive=true, Name=, ShortName=ShortName, TerminationDate=null, Type=XXXX, WasCreatedInError=false}, {Code=XXXXX, DataTimeStamp=2018-04-30T08:42:45Z, Description=, DocumentManagementStoreUrl=null, EffectiveDate=2013-12-10T00:00:00, IsActive=true, Name=, ShortName=ShortName, TerminationDate=null, Type=XXXX, WasCreatedInError=false}]

       

       

      • rakesh133's avatar
        rakesh133
        Occasional Contributor

        avidCoder Thanks for your update. Its working fine when I removed the Name and Description fields.

         

        However, for the testing purpose, this is not a feasible solution. I'm receiving this data via an API, I don't have the means to force any form of changing the data on the server side, this data is provided to me as-is.

         

        Could you suggest me a workaround for fixing this issue as I wont be able to manually copy the response and clear the fields each time when I want to run a test.

  • nmrao's avatar
    nmrao
    Champion Level 3
    It is difficult for the others to reproduce the issue without seeing the data. Please check with your team if it is internally developed service or service provider if it is external application.