Data with escaped double quotes from JSON response parsed as a string using Regex


Badge +1

I'm following the guide here on how to parse JSON response from a REST call to sharepoint: Parsing JSON returned from a REST service in Nintex workflow. It works as expected when the values being extracted do not contain double quotes. The problem happens when there's a double quote in the string I am extracting. 

 

Regex used: (?<="title":")([^"]*)(?=")

 

Scenario 1: No double quotes

Data to process: {"d":{"results":["title":"This is a title"]}}

Result: This is a title --> Passed

1850iE4A5B7349DC578D7.png

 

Scenario 2: With double quotes

Data to process: {"d":{"results":["title":"This is a ""pseudo"" title"]}}

Result using the regex above: This is a --> Failed

1851i8BAAEB893B379E91.png

 

I tried a lot of regex combination to handle such cases but when I try it using "Run Now", I get this result: [Command executed successfully.]

 

How do I handle such cases? I need to extract the whole string first before handling character escaping. I tried looking for other helpful tips but I can't make them work as well: Parse JSON Custom Action.

 

Environment:

  • SharePoint 2013
  • Nintex Workflow (on-premise)

 


6 replies

Userlevel 5
Badge +14

try following pattern

 

(?<="title":")(.*)(?="[,]}s])

 

1858iD1018AF7630F4608.jpg

Badge +1

Thanks for helping me out! The solution covers the double quotes. However, I forgot to mention that sometimes, users do not include a title since it is not required. When that happens, I get the [Command executed successfully.] response again see below.

1886i20AC873BBAF08F1F.png

Is it possible to handle this scenario in the same regex? If not, how to best handle this case?

Userlevel 5
Badge +14

and what output would you expect?

the message means the action executed but there is no match (resp. it matched an empty string)

 

1888i4BC67EE05F47D8F4.jpg

 

Badge +1
I am expecting a blank string. Does the "[Command executed successfully.]" return a blank string? That's something I haven't tried.
Userlevel 5
Badge +14

let's try in runtime :) 

Badge +1

I modified the regex a bit further. Basically, there's a lot of fields to be extracted and I was able to extract them all based on your suggested solution. Also, I was able to capture blank titles as well thanks to your responses.

 

Suggested regex:

(?<="title":")(.*)(?="[,]}s])

Final Regex:

(?<="title":")(.*)(?=","anotherField)

It was fortunate that JSON will always return "," anotherField format and that became the prefix of when I will be cutting the data. Since double quotes are getting escaped, I was able to capture the start and end of the values I am getting. Please note that it is only applicable for JSON keys that aren't the last in the JSON object since there is no "anotherField" for the last key. Since I don't need the value of the last key, I did not include it in my handling.

 

Thanks a lot for your help!

Reply