-1

how to format a stringified json object ?

JSON.stringify({a: 'avalue', b: 'bvalue'});

To output this :

{
    a: 'valuea',
    b: 'valueb'
}

instead of this :

{a:'valuea', b: 'valueb'}
Sofiene Djebali
  • 4,398
  • 1
  • 21
  • 27
  • 1
    Already answered [here](http://stackoverflow.com/questions/12950375/javascript-object-to-formatted-string/12951419#12951419) – mdickin May 20 '16 at 16:44
  • 1
    Wouldn't reading the docs for `JSON.stringify` normally be the first step? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – Jeremy J Starcher May 20 '16 at 16:45
  • [`[javascript] json indentation`](https://stackoverflow.com/search?q=%5Bjavascript%5D+json+indentation) – Felix Kling May 20 '16 at 16:47

1 Answers1

1

JSON.stringify function takes 3 parameters. The third one defines how many spaces will be inserted for indentation. So that you can use it:

JSON.stringify({a: 'avalue', b: 'bvalue'}, null, 2);
madox2
  • 49,493
  • 17
  • 99
  • 99