1

I try to send a POST request to my api using JSON, which has 2 parameters, both of which are string, but the 2nd parameter is a string consisting of an array. I use "", but the JSON can't be validated because it's wrong format. Example:

[
   {
      "command":"Light On",
      "electrodes_data":"[
         [
            -1.676484279858414084e+00,
            1.703593768412247300e+00,
            -2.212815768504515290e+01,
            -2.271715001249685884e+01,
            1.225172283709980547e+00,
            1.464548817242030054e+01,
            -9.198453203309327364e+00,
            1.003919234499335289e+00
         ],
         [
            2.207774024165701121e+00,
            1.350839771726168692e+01,
            -1.114531687390990555e+01,
            -1.165363114036154002e+01,
            7.126791623071767390e+00,
            1.072086845058947802e+01,
            1.251592868939042091e+00,
            8.341264304937794805e+00
         ],
         [
            7.394823959795758128e+00,
            1.485505312657915056e+01,
            -1.219862646958790720e+01,
            -1.363482670264784247e+01,
            8.551762457704171538e+00,
            1.213498147611971945e+01,
            5.537758254911750555e+00,
            9.852420400129631162e+00
         ]
      ]"
   },
   {
      "command":"Light Off",
      "electrodes_data":""[
         [
            1.093205724336439744e+01,
            1.664790142397396266e+01,
            -6.304287768318317831e+00,
            -9.288041036110371351e+00,
            1.448771805572323501e+01,
            2.803651912766508758e+01,
            2.190739106480032206e+01,
            5.626862201141193509e+00
         ],
         [
            8.181254264374729246e+00,
            1.139919802406802773e+01,
            -9.921244769473560154e+00,
            -1.219461815978866071e+01,
            5.934647234505973756e+00,
            2.065439357620198280e+01,
            1.782639012951403856e+01,
            4.578457863535732031e+00
         ],
         [
            1.189099969784729183e+01,
            1.185998179903253913e+01,
            2.910015485482290387e+00,
            1.989053226425312459e+00,
            1.672024700907059014e+00,
            2.774982235394418240e+00,
            1.299247698998078704e+01,
            6.204823252977803349e+00
         ],
         [
            2.212745977984741330e+01,
            5.191086520440876484e+00,
            3.033557224553078413e+00,
            1.221627268707379699e+00,
            1.990294787101447582e-01,
            -1.803207780583761632e+00,
            1.984525831416249275e+01,
            1.123637508391402662e+01
         ]
      ]"
   }
]

How to escape JSON to convert 2nd parameter to string?

Jérôme Teisseire
  • 1,518
  • 1
  • 16
  • 26
Elena
  • 11
  • 2

1 Answers1

0

You have an error because it's a JSON specification, you can read answer of James Gentes:

JSON doesn't allow breaking lines for readability.
Are multi-line strings allowed in JSON?

A online testing tool: Jsonlint for example.

you have to remove the " around the array because you can't have multi-line in JSON:

[{
    "command": "Light On",
    "electrodes_data": [
        [
            -1.676484279858414084e+00,
            1.703593768412247300e+00,
            -2.212815768504515290e+01,
            -2.271715001249685884e+01,
            1.225172283709980547e+00,
            1.464548817242030054e+01,
            -9.198453203309327364e+00,
            1.003919234499335289e+00
        ],
        [
            2.207774024165701121e+00,
            1.350839771726168692e+01,
            -1.114531687390990555e+01,
            -1.165363114036154002e+01,
            7.126791623071767390e+00,
            1.072086845058947802e+01,
            1.251592868939042091e+00,
            8.341264304937794805e+00
        ]
    ]

}, {
    "command": "Light Off",
    "electrodes_data": [
        [
            1.093205724336439744e+01,
            1.664790142397396266e+01,
            -6.304287768318317831e+00,
            -9.288041036110371351e+00,
            1.448771805572323501e+01,
            2.803651912766508758e+01,
            2.190739106480032206e+01,
            5.626862201141193509e+00
        ],
        [
            2.212745977984741330e+01,
            5.191086520440876484e+00,
            3.033557224553078413e+00,
            1.221627268707379699e+00,
            1.990294787101447582e-01,
            -1.803207780583761632e+00,
            1.984525831416249275e+01,
            1.123637508391402662e+01
        ]
    ]

}]
Jérôme Teisseire
  • 1,518
  • 1
  • 16
  • 26
  • I try to pass array as a string, because I don't want to use float in my api, this can break the precision of the data on the api side, how can I convert it to a string, is there any way to make it possible? – Elena Dec 12 '21 at 23:21
  • so, can you convert each float to a string `"-1.676484279858414084e+00",` ? to have an array of strings – Jérôme Teisseire Dec 12 '21 at 23:50
  • Well yes, I can convert it to a string. This is a good advise, but I have already decided to use float, and then I will create a mapper, which will convert it to a string. – Elena Dec 13 '21 at 00:31