2

I'm trying to add formatted json string to my textarea field. But it doesn't work.

I'm formatting string using this approach: How can I beautify JSON programmatically?

and then just call

textarea.setValue(formattedJson);

Check the snapshot of result:

enter image description here

Is it poosible to get correctly formatted string in the textarea?

Community
  • 1
  • 1
Andrey Selitsky
  • 2,584
  • 3
  • 28
  • 42

1 Answers1

9

The JSON.stringify method takes Object as parameter. You probably send string as parameter. This is correct code:

jsonObj = {a: 'b', c: [1,2,3]};
// note that jsonObj is object, not string
jsonString = JSON.stringify(jsonObj, null, '\t');

textarea.setValue(jsonString);

Here is jsfiddle

Molecular Man
  • 22,277
  • 3
  • 72
  • 89