Attention: Here be dragons
This is the
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Helper class for creating and parsing JSON data.
The JSON enables all data types to be converted to and from a JSON string. This useful for serializing data to save to a file or send over the network.
stringify is used to convert any data type into a JSON string.
parse is used to convert any existing JSON data into a Variant that can be used within Godot. If successfully parsed, use data to retrieve the Variant, and use
typeof to check if the Variant's type is what you expect. JSON Objects are converted into a Dictionary, but JSON data can be used to store Arrays, numbers, Strings and even just a boolean.
var data_to_send = ["a", "b", "c"] var json_string = JSON.stringify(data_to_send) # Save data # ... # Retrieve data var json = JSON.new() var error = json.parse(json_string) if error == OK: var data_received = json.data if typeof(data_received) == TYPE_ARRAY: print(data_received) # Prints array else: print("Unexpected data") else: print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
Alternatively, you can parse string using the static parse_string method, but it doesn't allow to handle errors.
var data = JSON.parse_string(json_string) # Returns null if parsing failed.
Note: Both parse methods do not fully comply with the JSON specification:
Trailing commas in arrays or objects are ignored, instead of causing a parser error.
New line and tab characters are accepted in string literals, and are treated like their corresponding escape sequences