0

I realize that the topic of converting Javascript to JSON and escaping correctly has been covered (for instance here, Convert JS object to JSON string, and here, How can I accommodate a string with both single and double quotes inside of it in Javascript). I'm not having much success at all, though --

I'm trying to pass a large Javascript object to a POST call (using Postman) that asks me to pass it in the following manner:

{ "key":"/url/url/es", "value":"{myData}" }

Where my object needs to fill the part of {myData}. My data structure is too large to pass here entirely, but it contains parts like this

 name: 'Something',
      reactComponentName: 'EntradaTwoPack',
      numberOfCards: 2,
      variant: '0',
      mainTitle: 'Something',
      cards: [
        {
          name: 'card1',
          elements: {
            showImage1: false,
            _comment: 'These fields are required, the component can expect them to know what to paint',
          },
          values: {
            cardTitle: '1 DÍA 1 PARQUE',
            cardLinkText: 'Condiciones',
            cardLinkURL: '/condiciones',
            images: ['https://s3-eu-west-1.amazonaws.com/anImage.png'],
            section0Title: 'Adulto',
            section0SubTitle: '11 - 59 años',
            section1Title: 'Júnior / Sénior',
            section1SubTitle: '4 - 10 años/+60 años',
        },
      ]

That is, it contains single quotes, URLs, objects inside arrays, objects in objects, etc. I've tried escaping the single quotes and the forward slashes in the URLs, removing the line breaks, etc, but nothing seems to work. (I end up with an output that resembles this:

mainTitle: \'TOP HOTELES\', mainDescription: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. \', mainLinkURL: \'http:\/\/www.startpage.com\', showBreadCrumb: false, cards: [ { name: \'card1\',

I've pasted it in the console of Chrome and then I JSON.stringify() it, but that didn't work either. I've also taken the output from Chrome and escaped it, as per above, but no dice. Perhaps I'm missing something conceptually -- any advice welcome!

Cerulean
  • 5,543
  • 9
  • 59
  • 111
  • 1
    could you post the actual part `Where my object needs to fill the part of {myData}` or how you add it to the post-request? because you should be fine just remove the doublequotes at `"{myData}"` and put your json there as postman doesnt expect "stringified" https://s3.amazonaws.com/postman-static-getpostman-com/postman-docs/58960775.png – john Smith Feb 21 '19 at 16:49
  • 2
    Does `JSON.stringify(jsObject);` work when you just input a valid javascript object without any additional escapes? – Adder Feb 21 '19 at 16:50
  • @johnSmith -- When you say 'put your json there', do you mean the object as such, without converting it to JSON? I tried `{ "key":"/url/url/es", "value":{ route: '/', title: 'home' } }` (just a short snippet) and Postman is flagging all the single quotes. Or do you mean I need to escape all the single quotes and forward slashes? I tried `{ "key":"/url/url/es", "value":{ route: \'\/\', title: \'home\' } }` and that didn't work either.... – Cerulean Feb 21 '19 at 16:58
  • @Adder -- no... – Cerulean Feb 21 '19 at 16:58
  • @Cerulean i guess you missed sth. , JSON aka Javascript Object Notation is vanilla JS objects. when you stringify them, its just a string aka not a js object anymore and you seem to be stuck in between, just pass js-object to postman, not string e.g starts with `{` not `"` – john Smith Feb 21 '19 at 17:03
  • @johnSmith -- Thanks. Yes, I've been stringifying them -- I was following some of the suggestions here to simply place the object (see the first answer below). If I'm passing JSON, I need to set the type to 'application/json', right? Anyway, I've been stringifying them (using both Chrome and online tools), escaping them, etc. – Cerulean Feb 21 '19 at 17:07

3 Answers3

2

The value must be a string?

Because you could just pass the hole object you have as an object and use your Content-Type as application/json

{
  "key":"/url/url/es", "value":YOU_OBJECT
}
gravatasufoca
  • 37
  • 1
  • 8
1

Try this - https://jsitor.com/4turZdkIJ, I just wrote a small stringifying utility for you

Replace your JSON object in myJSON object and run the snippet. It will print the thing you need in terminal.

The trick is, since you have to pass the stringified version of your object in value field, you should use JSON.stringify(myJSON) method of JavaScript. But since you are directly using Postman you may have to use some tool to convert that. The above link will help you to do that.

Ashvin777
  • 1,446
  • 13
  • 19
  • thanks very much -- However, Postman is flagging the first single quote (in `{ key: 'some data',`) with "Bad string".... – Cerulean Feb 21 '19 at 17:02
  • ok so that is because its not valid JSON, a valid JSON should not contain quotes and even keys also needs to be wrapper in quotes. Let me change the snippet. – Ashvin777 Feb 21 '19 at 17:03
1

In the end, simply using JSON.stringify() in Chrome, I was able to get it to work. I have no idea what the issue was the other times I tried that -- I was careful. Thanks to all who gave me their time!

Cerulean
  • 5,543
  • 9
  • 59
  • 111