Blitz3D+ Command Reference

JsonObjectKey$ ( json,index )

Parameters

json - JSON object handle

index - insertion-order position, 0 to JsonCount(json)-1

Description

Returns the object key at an insertion-order index.

Together with JsonCount, this lets you walk an object without knowing its keys upfront - dump an unknown save file for debugging, migrate old settings field by field, or list every profile section in a mod's config. Objects preserve insertion order, so the walk visits keys in the order they were added (which is also the order JsonStringify writes them).

The usual loop fetches each key, then reads its value with JsonObjectGet and inspects it with JsonKind.

The index must be in range: anything outside 0 to JsonCount(json)-1 is a programming error that stops the program, as is calling JsonObjectKey on a non-object. Note that replacing a key's value keeps its position, but removing a key and adding it back moves it to the end - so do not treat positions as stable across edits.

For the full rules, see the Application Data language reference.

Requires Extended mode.

See also: JsonCount, JsonObjectGet, JsonObjectHas, JsonKind, CreateJsonObject.

Example

; JsonObjectKey Example
; ---------------------
; Requires Extended mode.

q$=Chr(34)  ; Double-quote character, for building JSON text

; A loaded save-game record - objects preserve insertion order
save=ParseJson("{"+q+"player"+q+":"+q+"Aria"+q+","+q+"level"+q+":7,"+q+"gold"+q+":1250}")
Print "Save-game: "+JsonStringify(save,0)
Print ""

; JsonObjectKey returns the key at each insertion-order index,
; which lets you walk an object without knowing its keys upfront
Print "Fields in insertion order:"
For i=0 To JsonCount(save)-1
    Print "  index "+i+": "+JsonObjectKey(save,i)
Next

FreeJson save

Print ""
Print "Press any key to close the example"
WaitKey

End

Index