How can I beautify JSON inside a document?
Asked Answered
K

2

12

I was writing a LaTeX document, using Lyx as a front-end. I did not find a way to correctly format JSON data inside these documents.

How can I beautify or format JSON data inside a Latex document?

Keffiyeh answered 22/11, 2009 at 21:21 Comment(0)
M
-4

I do embed code into lyx mostly as program listings imported from external textfiles. There are numerous options that you could apply to the program listing, from line numbers to word wraps and many more.

Mcelhaney answered 22/11, 2009 at 21:29 Comment(3)
Autoformatting isn't one of those, though. At least listings doesn't have the option as the approach to prettifying is rather braindead anyway (just literal/keyword highlighting instead of understanding the grammar).Hull
I guess I'm missing something here. So, I've used listing to insert the JSON but what am I expecting here because I don't see any significant difference in the generated PDF. I mean, leave alone the keyword highlighting, I don't even see any formatting. Any inputs?Keffiyeh
Never mind. I missed the settings option. I'm playing with it now.Keffiyeh
C
39

I'm using minted package.

First you have to install and configure it (do not forget to add pygmentize.cmd file on Windows 7). Then you have to add \usepackage{minted} to the LaTeX Preambel and finally insert following code into TeX Code box (for JSON you can use js code type):

\begin{listing}
\begin{minted}[frame=single,
               framesep=3mm,
               linenos=true,
               xleftmargin=21pt,
               tabsize=4]{js}
{     
    "firstName": "John"
    "lastName" : "Smith",
    "age" : 25
}
\end{minted}
\caption{JSON example} 
\label{json-example}
\end{listing}

It is not perfect solution, but it works and produce really nice listing: minted json example

Christine answered 19/1, 2012 at 22:31 Comment(1)
There is also the code type {json}, producing a slightly different resultWrennie
M
-4

I do embed code into lyx mostly as program listings imported from external textfiles. There are numerous options that you could apply to the program listing, from line numbers to word wraps and many more.

Mcelhaney answered 22/11, 2009 at 21:29 Comment(3)
Autoformatting isn't one of those, though. At least listings doesn't have the option as the approach to prettifying is rather braindead anyway (just literal/keyword highlighting instead of understanding the grammar).Hull
I guess I'm missing something here. So, I've used listing to insert the JSON but what am I expecting here because I don't see any significant difference in the generated PDF. I mean, leave alone the keyword highlighting, I don't even see any formatting. Any inputs?Keffiyeh
Never mind. I missed the settings option. I'm playing with it now.Keffiyeh

© 2022 - 2024 — McMap. All rights reserved.