Repository navigation
Imported data model not playing nicely with \equal from ifthen #31
|
I'm trying to use jsonparse to read a json file that is supposed to contain customisation options for a document and then select various items to include in the document, depending on the options set. My thinking was that this should work with ifthen (or xifthen), through the ifthenelse command. However, I'm having a really hard time figuring out how the data are actually being stored and formatted so that I can use appropriate commands. For example, I can't work out even how to get it so that a string read from a JSON file is correctly computed to be equal to its value! (See the attached file for more details.) That's before I even get into doing boolean logic with imported booleans, or arithmetic with integers, both of which I would need to do. Any suggestions? Or is there some further development needed first? |
Replies: 4 comments
|
Thank you for your question, however I am a bit reluctant to open some .zip from an unknown source, but maybe you can create a minimal working example? If you need to grab a string from a JSON file or JSON data string for further processing, you might want to use the In general, the reason that testing with the
Therefore, you need to test against a The following is possible and should return "True": \documentclass{article}
\usepackage{jsonparse, ifthen}
\ExplSyntaxOn
\NewExpandableDocumentCommand { \tlToStr } { m } {
\tl_to_str:n {#1} % or \detokenize{#1}
}
\ExplSyntaxOff
\begin{document}
\JSONParse{\jsondata}{ { "string" : "foobarbaz" } }
\ifthenelse{
\equal{\JSONParseExpandableValue{\jsondata}{string}}{\tlToStr{foobarbaz}}
}{True}{False}
\end{document}Another way to do this is to use \documentclass{article}
\usepackage{jsonparse, ifthen}
\begin{document}
\JSONParse{\jsondata}{ { "string" : "foobarbaz" } }
\JSONParseValue[store in={\jsonstring}, rescan]{\jsondata}{string}
\ifthenelse{
\equal{\jsonstring}{foobarbaz}
}{True}{False}
\end{document} |
|
Okay, that's fair on not downloading zip files. I've figured out how to get a demonstration tex and a json to you -- GitHub doesn't take tex files as attachments, rather frustratingly. JSON: test.json But it sounds like you had inferred correctly what I was wanting to do. TeX isn't really my area in terms of coding expertise; I'm a modestly sensible LaTeX user but I'm not really in the low-level TeXnicalities of data types etc. I'll give your suggestion a go and see how that runs. Thanks! |
|
Having checked your code, I can only repeat what I said above. Since \def\str{\JSONParseExpandableValue{\test}{str}}
\def\strA{\detokenize{string}}Then, You don't need to do this if comparing integers, since digits and punctuation marks have catcode 12 per default. Another way could be to rescan what is returned by Note that you can just write I updated the documentation to hopefully clarify a bit better how the parsing works and which kind of return values the user can expect. |
|
Aha, thank you! The quotes came from my forgetting briefly that I was in LaTeX... I learnt LaTeX a long old time ago, and these days tend to do more work in Python.
|
Thank you for your question, however I am a bit reluctant to open some .zip from an unknown source, but maybe you can create a minimal working example?
If you need to grab a string from a JSON file or JSON data string for further processing, you might want to use the
\JSONParseExpandableValuecommand. I never tested this in combination with theifthenelsepackage, but I would assume that you need to feed it an expandable command (which also might need to be expanded first).In general, the reason that testing with the
\equalcommand has its hurdles is twofold:First, as stated in my previous comment and also in the package manual, if you want to further use the value you grab from JSON …