-1

I have a problem regardning my JSON. I want to send it to an REST-API Service but there is always an error.

JSON (written in C#):

var json = "{" +
                          "\"nachname\":\"Rindberger\","+
                          "\"anmerkung\":{"+
                                    "\"type\":\"multipart\"," +
                                    "\"content\":[ {" +
                                                "\"contentType\":\"application/pdf name=\"jakob.pdf\","+
                                                "\"contentDisposition\":\"attachment filename=\"jakob.pdf\","+
                                                "\"data\":\"" + file + "\"," +
                                                "\"contentTransferEncoding\":\"base64\"" +
                                                "}" +
                                    "]" +
                                    "}" +
                          "}";

I know this JSON looks very caotic but maybe somone has an idea why this json isn't working.

I tried this JSON in an JSON-Formatter, there comes an error message but I don't understand what this Error-Message means.

JSON-Formatter: enter image description here

I hope somebody can help me. Thanks in advance!

  • 1
    Are you sure filenames like `name=\"jakob.pdf\"` have to be surrounded with quotes? Try removing them and test your code again. – Jenea Vranceanu Jul 24 '20 at 08:10
  • I often find it easier to write JSON in code using a single quote `'` and the do a string `Replace('\'', '"')` on it. It makes the JSON easier to read in the .cs file. – Sean Jul 24 '20 at 08:12
  • You're missing a load of EOL commas for a start – Chris Pickford Jul 24 '20 at 08:13
  • Starting from line 7, your json is invalid. Your "contentType" value contains non escaped quotes. If you want to use quotes in that value you have to escape them. Here is a thread showing how to escape quotes: [How to escape double quotes in JSON](https://stackoverflow.com/questions/15637429/how-to-escape-double-quotes-in-json) – rekcul Jul 24 '20 at 08:13

1 Answers1

1

You are missing some escapes and quotes for json in name=\"jakob.pdf\" and filename=\"jakob.pdf\".

var json = "{" +
                      "\"nachname\":\"Rindberger\"," +
                      "\"anmerkung\":{" +
                                "\"type\":\"multipart\"," +
                                "\"content\":[ {" +
                                            "\"contentType\":\"application/pdf; name=\\\"jakob.pdf\\\"\"," +
                                            "\"contentDisposition\":\"attachment; filename=\\\"jakob.pdf\\\"\"," +
                                            "\"data\":\"" + "FILE" + "\"," +
                                            "\"contentTransferEncoding\":\"base64\"" +
                                            "}" +
                                "]" +
                                "}" +
                      "}";

Valid json should look something like this:

{
   "nachname":"Rindberger",
   "anmerkung":{
      "type":"multipart",
      "content":[
         {
            "contentType":"application/pdf; name=\"jakob.pdf\"",
            "contentDisposition":"attachment; filename=\"jakob.pdf\"",
            "data":"FILE",
            "contentTransferEncoding":"base64"
         }
      ]
   }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132