* [NTG-context] Json module and lua->context communication question
@ 2026-02-13 10:20 Andreas Hof via ntg-context
2026-02-13 12:24 ` [NTG-context] " Hans Hagen via ntg-context
2026-02-16 15:32 ` Aditya Mahajan
0 siblings, 2 replies; 7+ messages in thread
From: Andreas Hof via ntg-context @ 2026-02-13 10:20 UTC (permalink / raw)
To: ntg-context; +Cc: Andreas Hof
[-- Attachment #1.1: Type: text/plain, Size: 3972 bytes --]
Hi all here,
I have a question in the area of the json module and in general about the interaction between Lua and ConTeXt.
In m-json.mkxl we have for example:
interfaces.implement {
name = "tablefield",
actions = function(namespace,name)
local data = field(namespace,name)
if data then
context(data)
end
end,
arguments = "2 arguments",
public = true,
protected = true,
}
Means, that, if I ask for a table field from json file the value will be typeset through context(data).
Is there any possibility to check the value passed from Lua to ConTeXt on ConTeXt level?
I like to use the json module for getting addresses for a mass lettering.
Prominent examples, where a check of the value from a json-database is needed is the title of a person (typeset it plus a space or do nothing if empty),
or if you need to have alternatives for an opening sentence depending on the salutation or gender. Or whatever you can think of in a mass letter depending on the dataset ...
The idea is to do the special handlings on ConTeXt level, but I didn’t manage to get it running together with the existing json module.
Also checks against non empty field values don’t work for me.
I know that context(data) is not really returning a value, like a return value in procedural languages. But I hope there is a solution to do the conditionals on ConTeXt level.
What I also don’t understand is, why different variants of how the lua code is called, behave different, my expectation was that they simply run under a different catcode regime.
% typsets: equal (as expected)
\edef\compareVal{A}
\edef\temp{\ctxlua{context("A")}}
\ifx\temp\compareVal equal\else not equal\fi
But in the following variants I always branch to the \else path:
\edef\temp{\ctxluacode{context("A")}}
\ifx\temp\compareVal equal\else not equal\fi
\edef\temp{\startluacode context("A") \stopluacode}
\ifx\temp\compareVal equal\else not equal\fi
\edef\temp{\startlua context("A") \stoplua}
\ifx\temp\compareVal equal\else not equal\fi
The following mwe (files are also attached), shows one use case (address field in the letter)
I hope someone here, is able to help.
\usemodule[json]
\loadjsonfile[mglist][addr.json]
\showmakeup[space]
\starttext
% read all adresses
\dorecurse{\jsonlength{mglist}{records}}{%
% create macros for later use ...
\edef\Salutaion{\jsonfield{mglist}{records[\recurselevel].salutation}}
\edef\Title{\jsonfield{mglist}{records[\recurselevel].title}}
\edef\Name{\jsonfield{mglist}{records[\recurselevel].name}}
\edef\Firstname{\jsonfield{mglist}{records[\recurselevel].firstname}}
\edef\Street{\jsonfield{mglist}{records[\recurselevel].street}}
\edef\ZIP{\jsonfield{mglist}{records[\recurselevel].zip}}
\edef\Location{\jsonfield{mglist}{records[\recurselevel].location}}
% print the Adress ...
\Salutaion\space\ifx\Title\empty\else\Title\space\fi\Firstname\space\Name \blank[small]
\Street \blank[small]
\ZIP\space\Location
\blank[line]
}
% works ...
\edef\temp{\ctxlua{context("")}}
\ifx\temp\empty empty\else not empty\fi
% works ...
\edef\compareVal{A}
\edef\temp{\ctxlua{context("A")}}
\ifx\temp\compareVal equal\else not equal\fi
\stoptext
With a sample Json file (addr.json):
{
"records": [
{
"salutation": "Mrs.",
"title": "",
"name": "von Bingen",
"firstname": "Hildegard",
"street": "past road 1",
"zip": "00000",
"location": "Bingen am Rhein"
},
{
"salutation": "Mr.",
"title": "Dr.",
"name": "von Kos",
"firstname": "Hippokrates",
"street": "long ago road 1",
"zip": "00001",
"location": "Kos"
}
]
}
Thanks for help …
Andreas

[-- Attachment #1.2.1: Type: text/html, Size: 6329 bytes --]
[-- Attachment #1.2.2: addr.json --]
[-- Type: application/json, Size: 432 bytes --]
[-- Attachment #1.2.3: Type: text/html, Size: 352 bytes --]
[-- Attachment #1.2.4: jsontest.tex --]
[-- Type: application/octet-stream, Size: 1078 bytes --]
\usemodule[json]
\loadjsonfile[mglist][addr.json]
\showmakeup[space]
\starttext
% read all adresses
\dorecurse{\jsonlength{mglist}{records}}{%
% create macros for later use ...
\edef\Salutaion{\jsonfield{mglist}{records[\recurselevel].salutation}}
\edef\Title{\jsonfield{mglist}{records[\recurselevel].title}}
\edef\Name{\jsonfield{mglist}{records[\recurselevel].name}}
\edef\Firstname{\jsonfield{mglist}{records[\recurselevel].firstname}}
\edef\Street{\jsonfield{mglist}{records[\recurselevel].street}}
\edef\ZIP{\jsonfield{mglist}{records[\recurselevel].zip}}
\edef\Location{\jsonfield{mglist}{records[\recurselevel].location}}
% print the Adress ...
\Salutaion\space\ifx\Title\empty\else\Title\space\fi\Firstname\space\Name \blank[small]
\Street \blank[small]
\ZIP\space\Location
\blank[line]
}
% works ...
\edef\temp{\ctxlua{context("")}}
\ifx\temp\empty empty\else not empty\fi
% works ...
\edef\compareVal{A}
\edef\temp{\ctxlua{context("A")}}
\ifx\temp\compareVal equal\else not equal\fi
\stoptext
[-- Attachment #1.2.5: Type: text/html, Size: 352 bytes --]
[-- Attachment #1.2.6: jsontest.pdf --]
[-- Type: application/pdf, Size: 10906 bytes --]
[-- Attachment #1.2.7: Type: text/html, Size: 352 bytes --]
[-- Attachment #2: Type: text/plain, Size: 511 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 7+ messages in thread
* [NTG-context] Re: Json module and lua->context communication question
2026-02-13 10:20 [NTG-context] Json module and lua->context communication question Andreas Hof via ntg-context
@ 2026-02-13 12:24 ` Hans Hagen via ntg-context
2026-02-13 14:19 ` Andreas Hof via ntg-context
2026-02-16 15:32 ` Aditya Mahajan
1 sibling, 1 reply; 7+ messages in thread
From: Hans Hagen via ntg-context @ 2026-02-13 12:24 UTC (permalink / raw)
To: ntg-context; +Cc: Hans Hagen
On 2/13/2026 11:20 AM, Andreas Hof via ntg-context wrote:
> \edef\compareVal{A}
> \edef\temp{\ctxlua{context("A")}}
> \ifx\temp\compareVal equal\else not equal\fi
>
> But in the following variants I always branch to the \else path:
>
> \edef\temp{\ctxluacode{context("A")}}
> \ifx\temp\compareVal equal\else not equal\fi
\meaningful\ctxluacode
shows that it is (1) protected and (2) does more
> \edef\temp{\startluacode context("A") \stopluacode}
> \ifx\temp\compareVal equal\else not equal\fi
idem
> \edef\temp{\startlua context("A") \stoplua}
> \ifx\temp\compareVal equal\else not equal\fi
idem
> The following mwe (files are also attached), shows one use case (address
> field in the letter)
so just use \ctxlua which expands
> I hope someone here, is able to help.
also, there is:
- helper:
\doifelsesomething{\temp}{not empty}{empty}
- primitive \ifempty (also takes {})
\Salutaion\space\ifempty\Title\else\Title\space\fi\Firstname\space\Name
\blank[small]
- primitive \iftok
\iftok{\ctxlua{context("A")}}{A}\else not \fi equal
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 7+ messages in thread
* [NTG-context] Re: Json module and lua->context communication question
2026-02-13 12:24 ` [NTG-context] " Hans Hagen via ntg-context
@ 2026-02-13 14:19 ` Andreas Hof via ntg-context
2026-02-13 14:43 ` Hans Hagen via ntg-context
0 siblings, 1 reply; 7+ messages in thread
From: Andreas Hof via ntg-context @ 2026-02-13 14:19 UTC (permalink / raw)
To: mailing list for ConTeXt users; +Cc: Andreas Hof
Hello Hans,
thanks for the quick response.
However, the answer to just use \ctxlua as it expands, doesn’t help with the usage of the module m-json.mkxl (from the context distribution) which has all the interfaces in:
\startluacode
...
\stopluacode
So the simple question is: Is there a possibility to check the value passed to context by the \tablefield interface?
Or is it better to extend the module like this for example?
% first quick shot:
\startluacode
local field = utilities.json.field
interfaces.implement{
name = "tablefieldset",
actions = function(namespace, path, csname)
local data = field(namespace, path)
tex.sprint(string.format("\\gdef\\%s{%s}", csname, data))
end,
arguments = "3 arguments",
public = true,
protected = true,
}
\stopluacode
And then:
% in the loop: instead of \edef\Title{\jasonfield{}{}}
...
\tablefieldset{mglist}{records[\recurselevel].title}{Title} % creates a macro \Titel
...
\Salutaion\space\ifempty\Title\else\Title~\fi\Firstname\space\Name
Btw. Nice and very responsive group …
Thanks.
Andreas
Am 13.02.2026 um 13:24 schrieb Hans Hagen via ntg-context <ntg-context@ntg.nl>:
On 2/13/2026 11:20 AM, Andreas Hof via ntg-context wrote:
> \edef\compareVal{A}
> \edef\temp{\ctxlua{context("A")}}
> \ifx\temp\compareVal equal\else not equal\fi
> But in the following variants I always branch to the \else path:
> \edef\temp{\ctxluacode{context("A")}}
> \ifx\temp\compareVal equal\else not equal\fi
\meaningful\ctxluacode
shows that it is (1) protected and (2) does more
> \edef\temp{\startluacode context("A") \stopluacode}
> \ifx\temp\compareVal equal\else not equal\fi
idem
> \edef\temp{\startlua context("A") \stoplua}
> \ifx\temp\compareVal equal\else not equal\fi
idem
> The following mwe (files are also attached), shows one use case (address field in the letter)
so just use \ctxlua which expands
> I hope someone here, is able to help.
also, there is:
- helper:
\doifelsesomething{\temp}{not empty}{empty}
- primitive \ifempty (also takes {})
\Salutaion\space\ifempty\Title\else\Title\space\fi\Firstname\space\Name \blank[small]
- primitive \iftok
\iftok{\ctxlua{context("A")}}{A}\else not \fi equal
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 7+ messages in thread
* [NTG-context] Re: Json module and lua->context communication question
2026-02-13 14:19 ` Andreas Hof via ntg-context
@ 2026-02-13 14:43 ` Hans Hagen via ntg-context
2026-02-16 7:53 ` Andreas Hof via ntg-context
0 siblings, 1 reply; 7+ messages in thread
From: Hans Hagen via ntg-context @ 2026-02-13 14:43 UTC (permalink / raw)
To: ntg-context; +Cc: Hans Hagen
On 2/13/2026 3:19 PM, Andreas Hof via ntg-context wrote:
> Hello Hans,
>
> thanks for the quick response.
>
> However, the answer to just use \ctxlua as it expands, doesn’t help with the usage of the module m-json.mkxl (from the context distribution) which has all the interfaces in:
>
> \startluacode
> ...
> \stopluacode
>
> So the simple question is: Is there a possibility to check the value passed to context by the \tablefield interface?
> Or is it better to extend the module like this for example?
at the tex end
\doifelsetext{...}
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 7+ messages in thread
* [NTG-context] Re: Json module and lua->context communication question
2026-02-13 14:43 ` Hans Hagen via ntg-context
@ 2026-02-16 7:53 ` Andreas Hof via ntg-context
0 siblings, 0 replies; 7+ messages in thread
From: Andreas Hof via ntg-context @ 2026-02-16 7:53 UTC (permalink / raw)
To: ntg-context; +Cc: andreas.hof
Great, this one works!
Thanks a lot!
Andreas.
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 7+ messages in thread
* [NTG-context] Re: Json module and lua->context communication question
2026-02-13 10:20 [NTG-context] Json module and lua->context communication question Andreas Hof via ntg-context
2026-02-13 12:24 ` [NTG-context] " Hans Hagen via ntg-context
@ 2026-02-16 15:32 ` Aditya Mahajan
2026-02-20 8:09 ` Andreas Hof via ntg-context
1 sibling, 1 reply; 7+ messages in thread
From: Aditya Mahajan @ 2026-02-16 15:32 UTC (permalink / raw)
To: Andreas Hof via ntg-context; +Cc: Andreas Hof
On Fri, 13 Feb 2026, Andreas Hof via ntg-context wrote:
> Hi all here,
>
> I have a question in the area of the json module and in general about the interaction between Lua and ConTeXt.
>
> In m-json.mkxl we have for example:
>
> interfaces.implement {
> name = "tablefield",
> actions = function(namespace,name)
> local data = field(namespace,name)
> if data then
> context(data)
> end
> end,
> arguments = "2 arguments",
> public = true,
> protected = true,
> }
>
> Means, that, if I ask for a table field from json file the value will be typeset through context(data).
>
> Is there any possibility to check the value passed from Lua to ConTeXt on ConTeXt level?
>
> I like to use the json module for getting addresses for a mass lettering.
> Prominent examples, where a check of the value from a json-database is needed is the title of a person (typeset it plus a space or do nothing if empty),
> or if you need to have alternatives for an opening sentence depending on the salutation or gender. Or whatever you can think of in a mass letter depending on the dataset ...
For such tasks, one option is convert the data to a lua table, and then typeset using CLD. For example,
addresses = utilities.json.load("addr.json")
inspect(addresses)
gives
table={
["records"]={
{
["firstname"]="Hildegard",
["location"]="Bingen am Rhein",
["name"]="von Bingen",
["salutation"]="Mrs.",
["street"]="past road 1",
["title"]="",
["zip"]="00000",
},
{
["firstname"]="Hippokrates",
["location"]="Kos",
["name"]="von Kos",
["salutation"]="Mr.",
["street"]="long ago road 1",
["title"]="Dr.",
["zip"]="00001",
},
},
}
So, you can loop over the entries, check for title/salutation and write the logic in lua (no need to worry about expansion and stuff).
Aditya
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 7+ messages in thread
* [NTG-context] Re: Json module and lua->context communication question
2026-02-16 15:32 ` Aditya Mahajan
@ 2026-02-20 8:09 ` Andreas Hof via ntg-context
0 siblings, 0 replies; 7+ messages in thread
From: Andreas Hof via ntg-context @ 2026-02-20 8:09 UTC (permalink / raw)
To: ntg-context; +Cc: andreas.hof
Hello Aditya,
thanks for pointing to that option.
Until now I never tried it ...
Andreas
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-02-20 8:10 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-13 10:20 [NTG-context] Json module and lua->context communication question Andreas Hof via ntg-context
2026-02-13 12:24 ` [NTG-context] " Hans Hagen via ntg-context
2026-02-13 14:19 ` Andreas Hof via ntg-context
2026-02-13 14:43 ` Hans Hagen via ntg-context
2026-02-16 7:53 ` Andreas Hof via ntg-context
2026-02-16 15:32 ` Aditya Mahajan
2026-02-20 8:09 ` Andreas Hof via ntg-context
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).