How to check whether a key exists or not? #79
|
Sorry I am a begginner in latex. Take the In How can I check if the key or its value exist or not? I want to keep using I tested something like this but not successful \JSONParseFromFile{\myJSONdata}{example.json}
\ifx\JSONParseValue{\myJSONdata}{father}\empty
dosomething
\else
dosomethingelse
\JSONParseFromFile{\myJSONdata}{another-example.json}
\fiTo be exact, I am trying to manage secrets in json with sops in a latex project. Sorry I did not noticed the "Discussions" exists. |
Replies: 2 comments 1 reply
|
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 To test whether a key contains an empty value, you can test the value against \documentclass{article}
\usepackage{jsonparse}
\begin{document}
\JSONParse{\myjson}{ { "foo" : "" , "bar" : "baz" } }
\JSONParseValue[store in=\myvalueA]{\myjson}{foo}
\JSONParseValue[store in=\myvalueB]{\myjson}{bar}
\ifx\myvalueA\empty
empty
\else
not empty
\fi
\ifx\myvalueB\empty
empty
\else
not empty
\fi
\end{document}Since the JSON data is parsed into a property list, you can simply use \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 \documentclass{article}
\usepackage{jsonparse}
\begin{document}
\JSONParseFromFile{\myjson}{example.json}
\UseName{prop_if_in:NnF}\myjson{bar}{
\let\myjson\relax
\JSONParseFromFile{\myjson}{example-2.json}
}
...
\end{document}Also, you can't use the \documentclass{article}
\usepackage{jsonparse}
\begin{document}
\newif\ifparseother
\JSONParse{\myjson}{ { "foo" : "" } }
\UseName{prop_if_in:NnF}\myjson{bar}
{\parseothertrue}
\ifparseother
\let\myjson\relax
\JSONParse{\myjson}{ { "foo" : "" , "bar" : "baz" } }
\fi
\JSONParseValue{\myjson}{bar}
\end{document}Maybe I can add a user-level command named |
|
Thank you! It is extremely difficult to me, I don't even know what |
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):