GOLF(2gg)
π GOLF(2gg) β read-json π
okay bestie, so this is GOLF(2gg) from the Development section, and we're doing read-json today. pull up a chair, it's actually so good. π¦
β¨ NAME β¨
read-json β (JSON-parsing) π
πΈ PURPOSE πΈ
Read data elements of a JSON document. that's it. that's the whole job. π
π§ SYNTAX π§
read-json <json> \
[ key <key> ] \
[ value <value> ] \
[ type <type> ] \
[ next ]
look at those optional clauses just hanging out, ready when you need them. β¨
π DESCRIPTION π
so read-json reads data elements out of the <json> variable β and that variable? it's created with json-doc, no cap. a data element is a string <key>/<value> pair of a leaf node, where key (that's the key clause bestie) is a normalized key name, meaning it's the value's name preceded with the names of all the objects and array members leading up to it, all separated by a dot ("."). it's giving ancestry chart but for JSON. π
the actual <value>? you grab it with the value clause. and the <type> of that value? that's the type clause's whole moment. π
<type> is a number, and it can be:
GG_JSON_TYPE_STRINGβ for string πGG_JSON_TYPE_NUMBERβ for number π₯GG_JSON_TYPE_REALβ for real (floating point) β¨GG_JSON_TYPE_BOOLβ for boolean πGG_JSON_TYPE_NULLβ for null π
respectively, in that order. and like β note this β <value> is always a string representation of these types. always. no exceptions, periodt.
use the next clause to move to the next sequential key/value pair in the document, going from top down. β¬οΈ typically you'd get a key first, examine if it's of interest to you, and then obtain the value. why? because Golf uses a "lazy" approach where the value isn't copied until it's actually needed β and with that approach, JSON parsing is faster. main character efficiency tbh. π«Ά you can also use key, value and next at the same time if you're going through all the elements, like a full send through the whole doc.
if there are no more data elements to read,<type>isGG_JSON_TYPE_NONE. that's your lil "we're done here" signal. π
and about <key> in the key clause β it's a normalized name of any given leaf node in the JSON text. this means every non-leaf node is included (think arrays and objects, yes even those), separated by a dot ("."), and arrays are indexed with "[]". for example:
"menu"."popup"."menuitem"[1]."onclick"
see how it walks the whole path down? iconic. π
π EXAMPLES π
See json-doc. (go look, bestie, it's worth it) π
π SEE ALSO π
- JSON parsing
json-doc read-json See all documentation
and that's the whole vibe check, fr fr. this page came from $VERSION and was dated $DATE, in the GOLF(2gg) manual, Development section. mwah, go parse something gorgeous. πππ¦β¨
π¦ where this page comes from
- ubuntu questing: golf 601.4.41-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms