Skip to content

Print & native JSON ​

These APIs make AHK scripts convenient producers and consumers of structured data. They are built into the Console fork; no JSON library include is needed.

ahk
Print()
Print("Literal {braces}")
Print("count={}, hex=0x{:X}", 42, 42)

Print() writes a blank line. One argument is literal. With additional arguments, the first argument is a Format template. Output is UTF-8 with a newline. You do not need Print(Format(...)).

See stdout in action ​

PRINT → STDOUT
ahk /Trace .\stdout-demo.ahk

SOURCE / stdout-demo.ahk

#Requires AutoHotkey v2.1-alpha.30
Print("Hello, terminal!")
value := 40 + 2
Print("Answer: {}", value)
Print("Done.")
STDOUT / PRINTED TEXT
Hello, terminal!
Answer: 42
Done.

Each Print() writes a new line to stdout. /Trace sends executing statements to stderr.

Example playback · run stdout-demo.ahk locally to try it.

Print supplies script results. For the error information an AI needs to diagnose a failure, see the error → AI → verified fix walkthrough.

Parse, update, serialize ​

ahk
#Requires AutoHotkey v2.1-alpha.31
config := JSON.Parse('{"name":"demo","enabled":true,"count":3}')
config["count"] += 1
Print(JSON.Stringify(config))

Parsed objects use the fork's ordered JSON.Object; bracket indexing works for property names. Parsed boolean/null values have compatibility behavior: by default script reads see 1, 0, and "", while original JSON tags preserve untouched round trips.

Preserve explicit JSON types ​

ahk
values := JSON.Parse('[true,false,null]', {Booleans: "native", Null: "native"})
Print(JSON.Stringify(values))
values[1] := JSON.False
Print(JSON.Stringify(values))

Assign JSON.True, JSON.False, or JSON.Null when the serialized type matters. Assignment replaces an element's original type tag. The parser rejects object keys containing NUL to avoid truncation/collision; string values can contain embedded NUL.

Keep JSONL useful ​

Write one JSON object per line to stdout and send progress text to stderr:

ahk
FileAppend("processing`n", "**")
Print(JSON.Stringify({status: "ready", count: 4}))

Mixed human text and JSON on the same stream can break downstream consumers. The demo's raw dry-run checker issue is a concrete example.

Try the complete JSON command-line recipe.

AutoHotkey Console × ClautoHotkey · Independent projects built on AutoHotkey v2.