JSON

Inherits: Resource < RefCounted < Object

Helper class for creating and parsing JSON data.

Description

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.

Example

  1. var data_to_send = ["a", "b", "c"]
  2. var json_string = JSON.stringify(data_to_send)
  3. # Save data
  4. # ...
  5. # Retrieve data
  6. var json = JSON.new()
  7. var error = json.parse(json_string)
  8. if error == OK:
  9. var data_received = json.data
  10. if typeof(data_received) == TYPE_ARRAY:
  11. print(data_received) # Prints array
  12. else:
  13. print("Unexpected data")
  14. else:
  15. 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.

  1. 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 \n and \t.

  • Numbers are parsed using String.to_float which is generally more lax than the JSON specification.

  • Certain errors, such as invalid Unicode sequences, do not cause a parser error. Instead, the string is cleansed and an error is logged to the console.

Properties

Variant

data

null

Methods

int

get_error_line ( ) const

String

get_error_message ( ) const

String

get_parsed_text ( ) const

Error

parse ( String json_text, bool keep_text=false )

Variant

parse_string ( String json_string ) static

String

stringify ( Variant data, String indent=””, bool sort_keys=true, bool full_precision=false ) static


Property Descriptions

Variant data = null

Contains the parsed JSON data in Variant form.


Method Descriptions

int get_error_line ( ) const

Returns 0 if the last call to parse was successful, or the line number where the parse failed.


String get_error_message ( ) const

Returns an empty string if the last call to parse was successful, or the error message if it failed.


String get_parsed_text ( ) const

Return the text parsed by parse as long as the function is instructed to keep it.


Error parse ( String json_text, bool keep_text=false )

Attempts to parse the json_text provided.

Returns an Error. If the parse was successful, it returns @GlobalScope.OK and the result can be retrieved using data. If unsuccessful, use get_error_line and get_error_message for identifying the source of the failure.

Non-static variant of parse_string, if you want custom error handling.

The optional keep_text argument instructs the parser to keep a copy of the original text. This text can be obtained later by using the get_parsed_text function and is used when saving the resource (instead of generating new text from data).


Variant parse_string ( String json_string ) static

Attempts to parse the json_string provided and returns the parsed data. Returns null if parse failed.


String stringify ( Variant data, String indent=””, bool sort_keys=true, bool full_precision=false ) static

Converts a Variant var to JSON text and returns the result. Useful for serializing data to store or send over the network.

Note: The JSON specification does not define integer or float types, but only a number type. Therefore, converting a Variant to JSON text will convert all numerical values to float types.

Note: If full_precision is true, when stringifying floats, the unreliable digits are stringified in addition to the reliable digits to guarantee exact decoding.

The indent parameter controls if and how something is indented, the string used for this parameter will be used where there should be an indent in the output, even spaces like " " will work. \t and \n can also be used for a tab indent, or to make a newline for each indent respectively.

Example output:

  1. ## JSON.stringify(my_dictionary)
  2. {"name":"my_dictionary","version":"1.0.0","entities":[{"name":"entity_0","value":"value_0"},{"name":"entity_1","value":"value_1"}]}
  3. ## JSON.stringify(my_dictionary, "\t")
  4. {
  5. "name": "my_dictionary",
  6. "version": "1.0.0",
  7. "entities": [
  8. {
  9. "name": "entity_0",
  10. "value": "value_0"
  11. },
  12. {
  13. "name": "entity_1",
  14. "value": "value_1"
  15. }
  16. ]
  17. }
  18. ## JSON.stringify(my_dictionary, "...")
  19. {
  20. ..."name": "my_dictionary",
  21. ..."version": "1.0.0",
  22. ..."entities": [
  23. ......{
  24. ........."name": "entity_0",
  25. ........."value": "value_0"
  26. ......},
  27. ......{
  28. ........."name": "entity_1",
  29. ........."value": "value_1"
  30. ......}
  31. ...]
  32. }

Previous Next


© Copyright 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision 53e837c6.

Built with Sphinx using a theme provided by Read the Docs.