Forum Discussion

dwel's avatar
dwel
New Contributor
22 days ago
Solved

How do I prevent a file path from being altered when being parsed by JsonSlurper in Groovy Script?

I have a Groovy Script step which parses though a number of JSON objects from a REST response and saves certain parameters to be used in a following REST request. One of these parameters is the file path of the associated JSON object. Its format is: "C:\\ProgramData\\Folder1\\Folder2\\..."

The problem is that when I parse the JSON response using a JsonSlurper the file path format is altered to "C:\ProgramData\Folder1\Folder2\..." removing one of the '\' characters. This format is not accepted in the request I need it for and I cannot seem to find any information on how to prevent this from happening. Can anyone help?

  • I was able to find a solution. I added the following line to my groovy script to reformat the file path before saving it in the test case parameters. Heres the line:

    fullFilePath = fullFilePath.replaceAll("\\\\", "\\\\\\\\")

    because a '\' is considered a special character I had to represent it differently in the replaceAll method. The string "\\\\" actually represents a single '\' character and so by doubling the number to 8 backslash characters I was able to properly replace the single '\' with the double backslash again. 

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    dwel 

    Before anything, would you like to try using as path separator instead of \\ or \ and see if that helps?

    Thanks for the details, will go thru them later.

    • dwel's avatar
      dwel
      New Contributor

      I was able to find a solution. I added the following line to my groovy script to reformat the file path before saving it in the test case parameters. Heres the line:

      fullFilePath = fullFilePath.replaceAll("\\\\", "\\\\\\\\")

      because a '\' is considered a special character I had to represent it differently in the replaceAll method. The string "\\\\" actually represents a single '\' character and so by doubling the number to 8 backslash characters I was able to properly replace the single '\' with the double backslash again. 

  • nmrao's avatar
    nmrao
    Champion Level 3

    Please show the relevant script or problem. Or how would some one reproduce the same?

  • dwel's avatar
    dwel
    New Contributor

    I posted several screenshots above in my original post, I took these while in debug mode. Let me describe what is happening line by line:

    Line 4: The response from a previous Rest Request is assigned to the "buildingPlans" variable. This contains a number of plans and their parameters which I need to iterate through and save specific values if conditions are met. However the response is received in the format of an XML application/octet-stream.

    Lines 6, 7, 9, 10: I use the substring method to remove unwanted headers/text from the original response. At this point you can see in "buildingPlans" that the JSON objects have a parameter called "PlanFullPathName" note that the value of this parameter contains a file path with double backslashes: "\\

    Line 11: I create a JsonSlurper variable called "jsonreader" and pass my newly formatted "buildingPlans" String in.  Now that I have an ArrayList containing my JSON objects I can iterate through each object using an "eachWithIndex" loop. Note in the screenshot the highlighted line of jsonreader's object, the value of the PlanFullPathName now has only one backslash '\' instead of its previous double backslashes. This is the problem.

    I hope this helps you understand my problem, thanks for any help you can provide.