Taewan
Taewan

Reputation: 1227

Express.js Json Display Format

I am getting json data from mongodb and display it using res.json(data). The result looks like following human readable format.

{
    "glossary": {
        "title": "example glossary"
                 }
}

Is there a way I can display this in raw Json without blank spaces and put them in one line? For example, {"glossary":{"title":"example".....

Upvotes: 0

Views: 101

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074809

I find that very surprising (that it's rendering it that way). You might try res.send(JSON.stringify(data)). By default, JSON.stringify doesn't do any formatting.

Gratuitous JSON.stringify example:

var data = {
  "glossary": {
    "title": "example glossary"
  }
};
snippet.log(JSON.stringify(data));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Upvotes: 1

Related Questions