# LaTeX3 - Read Json as Input in LaTeX3

I would like to generate LaTeX template based on JSON Input. It is possible to take JSON as input in LaTeX3 and create LaTeX template? I had the solutions are found in LuaLaTeX.

Ways to parse JSON in LaTeX?

How do achieve this in LaTeX3? My JSON Input is below:

{
"LaTeXPage": {
"obj": "LaTeXPage",
"pro": {
"paperWidth": {
"property": "paperWidth",
"value": "129",
"units": "mm",
"remarks": null
},
"paperHeight": {
"property": "paperHeight",
"value": "198",
"units": "mm",
},
"topMargin": {
"property": "topMargin",
"value": "3",
"units": "mm",
},
}
}
}


Expected LaTeX Package output:

\setlength{\paperwidth}{129mm}
\setlength{\paperheight}{198mm}
\setlength{\topmargin}{3mm}


Especially i would like to use expl3 LaTeX Package. Or anyother solutions also welcome!!!

• Why don't you use LuaLaTeX and a real JSON parser library? Apr 18 '20 at 22:01
• @TeXnician: I have knowledge in LaTeX only and i'm new in LuaLaTeX. So i would like to do in LaTeX. Any solution greatly appreciate. Apr 19 '20 at 3:32
• @TeXnician: I had tried many ways and not found the solution. Could you please provide LuaLaTeX solution? Apr 23 '20 at 16:58

I have found the solution.

\documentclass{article}
\usepackage{luatextra} % luacode
\usepackage{environ}  % LaTeX Stylefile Writing in NewEnvironment

\begin{luacode*}
local handler = io.open(File, "rb")
handler:close()
return content
end

\end{luacode*}

\newcommand*\JSONpagewidth{\directlua{tex.print(tab['LaTeXPage']['pro']['pageWidth']['value'])}\directlua{tex.print(tab['LaTeXPage']['pro']['pageWidth']['units'])}}

\newwrite\writer
\NewEnviron{styleFileWrite}
{\immediate\write\writer{%
\string\setlength\paperwidth{\JSONpagewidth}^^J%
\unexpanded\expandafter{\BODY}
}}
\immediate\openout\writer=LaTeX-\jobname.sty
\begin{styleFileWrite}
\end{styleFileWrite}
\immediate\closeout\writer

\begin{document}

Pagewidth values is ::  {\JSONpagewidth}

\end{document}