I'm writing my recipes (food) in a small textfile in JSON. Are there any ways to parse this in LaTeX?
I know, that are lots of templates dealing with recipes, but I want to have my own style, which can be change very fast e.g. my mother is not interested in stuff like calories 🙂
Here is one simple example:
{
"recipe": {
"title":"First recipe",
"source":"My first cookbook",
"carbs":"1 oz",
"fat":"1 oz",
"protein":"1 oz",
"cal":"100 kcal",
"ingredients": [
{"item":"Eggs"},
{"item":"Oil"},
{"item":"Nuts"}
],
"cooking": [
{"step":"Mix eggs and oil"},
{"step":"Add nuts"}
]
}
}
Best Answer
Although the question is about parsing JSON in LaTeX, since the OP wants to "have my own style, which can be change very fast", I'll give a ConTeXt solution for its simplicity.
ConTeXt already comes up with a parser for JSON. To use it, simply load
Then, you can use the Lua function
utilities.json.tolua
to convert JSON string to Lua table and the Lua functionutilities.json.tostring
to convert a Lua table to a JSON string.It is very simple to typeset Lua tables using ConTeXt Lua Document. Here is a complete example:
Now you can simply define a TeX macro to pass its argument to the Lua function.
Let's add some minimal styling to format the section heads. As with all ConTeXt documents, you can change the format by using appropriate
\setup...
command.Finally, the main document
which gives