0

I have a request JSON that other system has given to use. It looks like below:

{
  \"list\": [
    {
      \"m1\": \"tval\",
      \"fill\": {
        \"tool\": \"4535\"
      },
      \"respfield\": [
        \"tid\",
        \"tName\"
      ]
    }
  ]
}

Note that it has back slash in front of every key and value. I am unable to parse it directly in https://jsonformatter.org/ with back slashes. When I remove the back slashes and format the JSON I get:

{
  "list": [
    {
      "m1": "tval",
      "fill": {
        "tool": "4535"
      },
      "respfield": [
        "tid",
        "tName"
      ]
    }
  ]
}

I used curl command to hit the service with back slash JSON -> returns a response and without back slash JSON- which returns error. My question is how can I use the JSON with back slash to generate an Apex request class that I can serialize. Should I be using the one without back slashes( but the service retruns error when used with curl command)? would JSON serialize and curl behave differently?

1 Answer 1

3

When you invoke curl from the command line, you have to escape any double quotes that you include within any quoted string. The escaped quotes are interpreted by your shell (probably bash), not by curl itself or by your remote service.

Your "real" outgoing JSON shouldn't include any backslashes. You should only have those present when you're embedding a JSON payload inside an outer quoted string in a context like bash or many other scripting languages.

2
  • Thanks. Thats what I thought but dint know the exact reason. So I assume I should be good with generating an apex class from JSON without back slash and using it when doing JSON serialization.
    – SfdcBat
    Commented Nov 7, 2018 at 18:02
  • 1
    That is correct.
    – David Reed
    Commented Nov 7, 2018 at 18:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .