Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing (or unescaping JSON) with nodejs and jade

Tags:

pug

I've been digging through Jade documentation and google searches but nothing words. I'm attempting to print a JSON string with a view but no matter how I try to print the variable, the double quotes are always converted to "

Update: I'm using Jade 0.26.0.

My test json:

{test:"value"}

My template code:

!{JSON.stringify(json)}

Raw output in the web browser:

{"test":"value"}
like image 331
helion3 Avatar asked May 25 '12 03:05

helion3


People also ask

How do I prettify JSON in node JS?

If you just want to pretty print an object and not export it as valid JSON you can use console. dir() . It uses syntax-highlighting, smart indentation, removes quotes from keys and just makes the output as pretty as it gets.

How do I prettify JSON?

Use JSON. stringify(obj) method to convert JavaScript objects into strings and display it. Use JSON. stringify(obj, replacer, space) method to convert JavaScript objects into strings in pretty format.

What is nodejs and JSON?

js is a JavaScript runtime built on Chrome's V8 JavaScript engine. With node. js as backend, the developer can maintain a single codebase for an entire application in javaScript. JSON on the other hand, stands for JavaScript Object Notation.

Why we use JSON in node?

JSON exists as a string — useful when you want to transmit data across a network. It needs to be converted to a native JavaScript object when you want to access the data. This is not a big issue — JavaScript provides a global JSON object that has methods available for converting between the two.


1 Answers

Maybe upgrade to more recent version of Jade?

I'm using version 0.25.0 and..

This is escaped

=JSON.stringify(item)

These are unescaped

!{JSON.stringify(item)}
!=JSON.stringify(item)
like image 104
250R Avatar answered Sep 23 '22 08:09

250R