Skip to content

Could there be an expandable ArrayMap? #84

Closed

Description

@teepeemm

In the following,

\documentclass{article}
\usepackage{jsonparse}
\JSONParse{\myJSONdata}{{"array":["a-b"]}}
\typeout{Line 4: \JSONParseValue{\myJSONdata}{array[0]}}
\typeout{Line 5: \JSONParseExpandableValue{\myJSONdata}{array[0]}}
\ExplSyntaxOn
\newcommand{\splitValues}{
 \item original: ~ \JSONParseArrayValue
 \seq_set_split:Nne \l_tmpa_seq { - } { \JSONParseArrayValue }
 \item using~seq~items: ~ \seq_item:Nn \l_tmpa_seq {1} ~ and ~ \seq_item:Nn \l_tmpa_seq {2}
 \exp_args:NNe \str_set:Nn \l_tmpa_str { \seq_item:Nn \l_tmpa_seq {1} }
 \exp_args:NNe \str_set:Nn \l_tmpb_str { \seq_item:Nn \l_tmpa_seq {2} }
 \item using~tmp~variables: ~ \l_tmpa_str{} ~ and ~ \l_tmpb_str
}
\ExplSyntaxOff
\begin{document}
\begin{itemize}
 \JSONParseArrayMapFunction{\myJSONdata}{array}{\splitValues}
 \JSONParseArrayMapFunction[rescan=false]{\myJSONdata}{array}{\splitValues}
\end{itemize}
\end{document}

we see that \typeout needs to have an expandable value: the log shows

Line 4: \JSONParseValue {\__prop_flatten:w \s__prop {$}}{array[0]}
Line 5: a-b

It seems that the \JSONParseArrayMapFunction may also need an expandable context. If I try to split \JSONParseArrayValue, then I end up seeing \jsonparse_rescan:e {<\JSONParseArrayValue>}, and the splitting doesn't work. In my case, I was able to specify rescan=false for the \JSONParseArrayMapFunction to have everything work out, but would it be possible to have \JSONParseExpandableArrayMapFunction or \JSONParseExpandableArrayValue? You mentioned in #54 that \JSONParseArrayMapInline is not expandable. Does this mean array maps are inherently non-expandable? Would I have been able to split if I had also needed to rescan?

Image

Activity

  1. jasperhabicht commented on Jan 19, 2026

    @jasperhabicht
    Owner

    Expandability in the context of handling arrays has been an issue from the very start and I tried to improve the existing functions already. It seems I need to revisit this again, but it may take some time. For now, this is not yet an answer, but only some first thoughts:

    Note that \JSONParseArrayMapInline is indeed not fully expandable (and of course it depends on the inline code), but depending on the context, you should in most cases still be able to use it inside a list or tabular.

    In some cases you can also try to use the store in key to save the result of the function in a token list variable that you can then use in other ways (see the PGFPlots example in the doc).

  2. jasperhabicht commented on Jan 19, 2026

    @jasperhabicht
    Owner

    Now that I read your issue again in more detail, it rather seems to be an problem with rescanning. Due to the fact that \tl_rescan:nn is not expandable, there is no straight-forward way to process an expandable and also rescanned token list. The typical way to overcome this problem is to store the rescanned result in another token list variable and then use this.

    You can make use of \jsonparse_rescan:n and/or \jsonparse_set_rescan:Nn to have better control about when and where the rescanning takes place:

    \documentclass{article}
    \usepackage{jsonparse}
    \JSONParse{\myJSONdata}{{"array":["\\textbf{a}-b"]}}
    
    \ExplSyntaxOn
    \newcommand{\splitValues}{
     \item original: ~ \jsonparse_rescan:n { \JSONParseArrayValue }
     \jsonparse_set_rescan:Nn \l_tmpa_tl { \JSONParseArrayValue }
     \seq_set_split:NnV \l_tmpa_seq { - } \l_tmpa_tl
     \item using~seq~items: ~ \seq_item:Nn \l_tmpa_seq {1} ~ and ~ \seq_item:Nn \l_tmpa_seq {2}
    }
    \ExplSyntaxOff
    
    \begin{document}
    \begin{itemize}
     \JSONParseArrayMapFunction[rescan=false]{\myJSONdata}{array}{\splitValues} 
    \end{itemize}
    \end{document}
  3. Repository owner locked and limited conversation to collaborators on Jan 19, 2026
  4. converted this issue into a discussion #86 on Jan 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions