0

I have a well formatted json like below:

{
    "Slide":[
        {
            "Code": "'!!!", 
            "Status": "OK"
        },
        {
            "Code": "123", 
            "Status": "Failed"
        }
    ]
}

I use json path to filter Status based on Code. The Jsonpath is like this : ..[?(@.Code == '{0}')].Status in C#. So programmatically parameter in jpath is populated. When value "'!!!" is passed to above jpath( i.e ..[?(@.Code == ''!!!')].Status ) it'll not work, as single quote is not escaped. Since I'm, not hardcoding parameter value I cannot escape it manually. i.e ..[?(@.Code == '\'!!!')].Status will solve the problem. But I need to achieve it programmatically.

One way to approach this is to use a regex and identify the pattern and escape it before passing the parameter to jpath. I'm not so good with regex and couldn't write one which will keep beginning and ending quote of jpath intact while replacing. Could anyone please help.

Or is there any better way to approach?..

P.s: I can't use regex feature of jpath as I need to filter by specific code.

SKN
  • 520
  • 1
  • 5
  • 20
  • If you need to achieve it programmatically - why don't you replace it programmatically: `$"..[?(@.Code == '{yourExpectedCode.Replace("'", @"\'")}')].Status"` ? – vasily.sib Apr 15 '19 at 04:22
  • The problem with the escaping like above code is that it'll replace beginning and ending quotes in Code == '{0}' . – SKN Apr 15 '19 at 04:49
  • No, it will not. Why do you think it will? – vasily.sib Apr 15 '19 at 04:51
  • Ok, I think you are just unfamiliar with [string intrpolation](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/string-interpolation). Just show us how do you replace `{0}` with `'!!!` in your code? – vasily.sib Apr 15 '19 at 04:53
  • Sorry, I think I was not clear. I'm aware of string interpolation, But I overlooked part of your code where you surrounded code to replaced with ' ' . However I can't use this because I wouldn't know the `code` before hand. Not all `code` requires escaping. Only when code has a single quote it needs be escaped. – SKN Apr 15 '19 at 05:51
  • 1
    You need to escape **any** single quote in `yourExpectedCode`, right? If there is no single quote in `yourExpectedCode` - nothing will be escaped, so what's the deal? I mean `"'!!!".Replace("'", @"\'") == @"\'!!!"` and`"A!!!".Replace("'", @"\'") == "A!!!"` – vasily.sib Apr 15 '19 at 05:56
  • Thanks for your patience. I was so dumb! – SKN Apr 15 '19 at 06:11
  • It's OK, we all sometimes have hard-days:) – vasily.sib Apr 15 '19 at 06:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191835/discussion-between-skn-and-vasily-sib). – SKN Apr 15 '19 at 06:53

0 Answers0