Skip to content

Imported data model not playing nicely with \equal from ifthen #28

Closed

Description

@WoosterUK

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?

test.zip

Activity

  1. jasperhabicht commented on Oct 26, 2024

    @jasperhabicht
    Owner

    Thank you for your question, however I am a bit relictant 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 \JSONParseExpandableValue command. I never tested this in combination with the ifthenelse package, but I would assume that you need to feed it an expandable command (which also might needs to be expanded first).

    I will test this later with some possible combinations.

  2. jasperhabicht commented on Oct 26, 2024

    @jasperhabicht
    Owner

    The reason that testing with the \equal command has its hurdles is twofold:

    1. 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 data via the jsonparse package, you need to use \JSONParseExpandableValue.

    2. Secondly, if you use \JSONParseExpandableValue, you need to know that the result is a str with specific catcodes. A str (string) has catcode 12 for all chararctes except spaces, while a tl (token list) has catcode 11 for letters. I should probably make this clear in the package manual (see Clarify output of \JSONParseExpandableValue #29 ).

    Therefore, you need to test against a str. You can make use of the \tl_to_str:n command (or create a user command via \NewExpandableDocumentCommand) to convert a tl to a str.

    The following is possible and should return "True":

    \documentclass{article}
    \usepackage{jsonparse, ifthen}
    
    \ExplSyntaxOn
    \NewExpandableDocumentCommand { \tlToStr } { m } {
        \tl_to_str:n {#1}
    }
    \ExplSyntaxOff
    
    \begin{document}
    
    \JSONParse{\jsondata}{ { "string" : "foobarbaz" } }
    
    \ifthenelse{
        \equal{\JSONParseExpandableValue{\jsondata}{string}}{\tlToStr{foobarbaz}}
    }{True}{False}
    
    \end{document}
  3. WoosterUK commented on Oct 26, 2024

    @WoosterUK
    Author

    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
    TEX: https://pastebin.com/yaJMmFJL

    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!

  4. jasperhabicht commented on Oct 26, 2024

    @jasperhabicht
    Owner

    Having checked your code, I can only repeat what I said above.

    Since \tl_to_str:n is essentially just \detokenize, you can define as follows:

    \def\str{\JSONParseExpandableValue{\test}{str}}
    \def\strA{\detokenize{string}}

    Then, \ifthenelse{\equal{\str}{\strA}}{true}{false} will return "true".

    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 \JSONParseExpandableValue{\test}{str}, but this is probably not expandable, so it won't directly work with \ifthenelse.

    Note that you can just write \JSONParseFromFile{\test}{test.json} without quotation marks around the file name. I am actually unsure why it also works with quotes ...

  5. jasperhabicht commented on Oct 28, 2024

    @jasperhabicht
    Owner

    I updated the documentation to hopefully clarify a bit better how the parsing works and which kind of return values the user can expect.

  6. Repository owner locked and limited conversation to collaborators on Oct 29, 2024
  7. converted this issue into a discussion #31 on Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions