You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To be exact, I am trying to manage secrets in json with sops in a latex project.
When the json file is encrypted, it have an extra key called sopslike this and it is not ready to be used.
When the json file is decrypted, that sops key disappeared, and it is ready to be used.
Hi, thank you for your enquiry! It is fine to post it here as it could be an enhancement request. Your code tests whether the value assigned to the key father exists, but not whether this key exists altogether.
To test whether a key contains an empty value, you can test the value against \empty (similar like what you tried in your example, but you need to store the parsed value into some token variable that you then can use for comparison):
Since the JSON data is parsed into a property list, you can simply use \prop_if_in:NnTF to test whether a key exists in this property list, for example like this:
\documentclass{article}
\usepackage{jsonparse}
\begin{document}
\JSONParse{\myjsonA}{ { "foo" : "" , "bar" : "baz" } }
\JSONParse{\myjsonB}{ { "foo" : "" } }
\UseName{prop_if_in:NnTF}\myjsonA{bar}{key exists}{key does not exist}
\UseName{prop_if_in:NnTF}\myjsonB{bar}{key exists}{key does not exist}
\end{document}
Note that you can't simply overwrite a property list but you need to \relax it first:
Also, you can't use the \JSONParse command as argument to another command (for the \JSONParseFromFile command, this issue however does not apply, I only add this as a reminder). You would need to do something along the following lines:
Thank you! It is extremely difficult to me, I don't even know what \UseName\let\relax\prop_if_in:NnTF are, even after some googling.
But it just works using your code!!
Thank you so much
Using \UseName, you can easily use commands defined by the LaTeX3 layer which has a lot of useful commands and also introduces to LaTeX the concept of property lists which the jsonparse package makes use of. (Because commands of the LaTeX3 layer have a special syntax, you can't use them just like this in normal code.) A property list essentially contains key-value pairs. In the context of the jsonparse package, this concept is used to store the JSON data as key-value pairs in such a property list. For example the JSON data { "foo" : "bar" } would be parsed into a property list that has exactly one entry with the key foo and the corresponding value bar.
The command \prop_if_in:NnTF checks whether the key given as second argument exists in the property list given as first argument. If yes (true branch, T), it will execute the code given to the third argument. If not (false branch, F), it will execute the code given to the fourth argument. There exist also the commands \prop_if_in:NnT and \prop_if_in:NnF that only have a true or false branch respectively.
The commands \relax and \let are commands that you should probably get familiar with as they will pop up often when working with TeX. Using \let, you can assign a command the meaning of another. With \let\foo\relax you essentially say that \foo shall be an exact copy of \relax and both do the same thing when expanden. Now, \relax essentially just means "do nothing". So, the above means: let \foo do nothing (and by this overwrite its possible prior assignment).
Repository owner locked and limited conversation to collaborators on Sep 3, 2025
Activity
jasperhabicht commentedon Sep 3, 2025
Hi, thank you for your enquiry! It is fine to post it here as it could be an enhancement request. Your code tests whether the value assigned to the key
fatherexists, but not whether this key exists altogether.To test whether a key contains an empty value, you can test the value against
\empty(similar like what you tried in your example, but you need to store the parsed value into some token variable that you then can use for comparison):Since the JSON data is parsed into a property list, you can simply use
\prop_if_in:NnTFto test whether a key exists in this property list, for example like this:Note that you can't simply overwrite a property list but you need to
\relaxit first:Also, you can't use the
\JSONParsecommand as argument to another command (for the\JSONParseFromFilecommand, this issue however does not apply, I only add this as a reminder). You would need to do something along the following lines:Maybe I can add a user-level command named
\JSONParseKeyExistsTFthat is essentially just a wrapper around\prop_if_in:NnTFif this is required.amalgame21 commentedon Sep 3, 2025
Thank you! It is extremely difficult to me, I don't even know what
\UseName\let\relax\prop_if_in:NnTFare, even after some googling.But it just works using your code!!
Thank you so much
jasperhabicht commentedon Sep 3, 2025
Let me shortly answer this:
Using
\UseName, you can easily use commands defined by the LaTeX3 layer which has a lot of useful commands and also introduces to LaTeX the concept of property lists which thejsonparsepackage makes use of. (Because commands of the LaTeX3 layer have a special syntax, you can't use them just like this in normal code.) A property list essentially contains key-value pairs. In the context of thejsonparsepackage, this concept is used to store the JSON data as key-value pairs in such a property list. For example the JSON data{ "foo" : "bar" }would be parsed into a property list that has exactly one entry with the keyfooand the corresponding valuebar.The command
\prop_if_in:NnTFchecks whether the key given as second argument exists in the property list given as first argument. If yes (true branch,T), it will execute the code given to the third argument. If not (false branch,F), it will execute the code given to the fourth argument. There exist also the commands\prop_if_in:NnTand\prop_if_in:NnFthat only have a true or false branch respectively.The commands
\relaxand\letare commands that you should probably get familiar with as they will pop up often when working with TeX. Using\let, you can assign a command the meaning of another. With\let\foo\relaxyou essentially say that\fooshall be an exact copy of\relaxand both do the same thing when expanden. Now,\relaxessentially just means "do nothing". So, the above means: let\foodo nothing (and by this overwrite its possible prior assignment).