GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

πŸ’– 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> is GG_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