4

Ok, I want the response to be a plain string, not JSON.

Ex. this object

let obj = {
  foo: 'bar',
  baz: 1
}

Should be returned as

"{foo: 'bar', baz: 1}"

Instead of

{"foo": "bar", "baz": 1}

Why? I need to use the string as a link in quickchart

<img src="https://quickchart.io/chart?c={type:'line',data:{labels:['January','February', 'March','April', 'May'], datasets:[{label:'Dogs', data: [50,60,70,180,190], fill:false,borderColor:'blue'},{label:'Cats', data:[100,200,300,400,500], fill:false,borderColor:'green'}]}}">

The double quotes in JSON break the image link.

Or, maybe suggest a better way.

4
  • 1
    JSON is always a string (and "{foo: 'bar', baz: 1}" isn't JSON) Commented Feb 18, 2020 at 12:11
  • Write your own serialiser, it's not difficult if your object property values are just strings and numbers. Commented Feb 18, 2020 at 12:12
  • 1
    @Andreas — But not a string in the format the OP is asking for. "{foo: 'bar', baz: 1}", as you said, isn't JSON, but it is what the OP wants. Commented Feb 18, 2020 at 12:12
  • try to use encodeURIComponent(JSON.stringify(obj)). Make sure that the response type is 'plain/text' Commented Feb 18, 2020 at 12:15

3 Answers 3

9

The double quotes in JSON break the image link.

You have an XY problem.

The problem is that using " characters in an attribute value delimited by " characters will break the attribute value.

While avoiding using " characters in the data is a solution, it isn't a good one (largely because avoiding them without breaking other stuff is hard). A better solution is to correctly escape them.

const json = JSON.stringify(obj);
const url_escaped_json = encodeURIComponent(json);
const img = `<img src="https://quickchart.io/chart?c=${url_escaped_json}" alt="...">`;

Note that this doesn't use HTML escaping (which would replace " with &quot;), because you are putting the data into a URL so it needs URL escaping first (and that will replace " with %20 rendering the HTML escaping unneeded).

Sign up to request clarification or add additional context in comments.

1 Comment

Perfection! Thank you for the great explanation. Love the XY problem pointer.
0

You might leverage a dedicated API for that like Image-Charts that handles such encoding issue nicely:

https://image-charts.com/chart?cht=lc
&chd=a:50,60,70,180,190|100,200,300,400,500
&chs=900x400
&chdl=Dogs|Cats&chdlp=t&chdls=444444,15
&chxt=x,y
&chxl=0:|January|February|March|April|May
&chg=1,1
&chco=0018F5,377E22

Disclaimer: I'm Image-Charts founder.

Comments

-4

You can use JSON.stringify for this.

let obj = {
  foo: 'bar',
  baz: 1
}

console.log(JSON.stringify(obj));

This will convert the object into string.

1 Comment

A string, yes. The string format the OP is asking for: No.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.