Table of Contents

Class Json

Namespace
Godot
Assembly
GodotSharp.dll

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(Variant, string, bool, bool) is used to convert any data type into a JSON string.

Parse(string, bool) 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

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 ParseString(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 \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.

[GodotClassName("JSON")]
public class Json : Resource, IDisposable
Inheritance
Json
Implements
Inherited Members

Constructors

Json()

public Json()

Properties

Data

Contains the parsed JSON data in Variant form.

public Variant Data { get; set; }

Property Value

Variant

Methods

GetErrorLine()

Returns 0 if the last call to Parse(string, bool) was successful, or the line number where the parse failed.

public int GetErrorLine()

Returns

int

GetErrorMessage()

Returns an empty string if the last call to Parse(string, bool) was successful, or the error message if it failed.

public string GetErrorMessage()

Returns

string

GetParsedText()

Return the text parsed by Parse(string, bool) as long as the function is instructed to keep it.

public string GetParsedText()

Returns

string

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

Parse(string, bool)

Attempts to parse the jsonText provided.

Returns an Error. If the parse was successful, it returns Ok and the result can be retrieved using Data. If unsuccessful, use GetErrorLine() and GetErrorMessage() for identifying the source of the failure.

Non-static variant of ParseString(string), if you want custom error handling.

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

public Error Parse(string jsonText, bool keepText = false)

Parameters

jsonText string
keepText bool

Returns

Error

ParseString(string)

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

public static Variant ParseString(string jsonString)

Parameters

jsonString string

Returns

Variant

Stringify(Variant, string, bool, bool)

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 fullPrecision 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:

## JSON.stringify(my_dictionary)
  {"name":"my_dictionary","version":"1.0.0","entities":[{"name":"entity_0","value":"value_0"},{"name":"entity_1","value":"value_1"}]}

JSON.stringify(my_dictionary, "\t")

{ "name": "my_dictionary", "version": "1.0.0", "entities": [ { "name": "entity_0", "value": "value_0" }, { "name": "entity_1", "value": "value_1" } ] }

JSON.stringify(my_dictionary, "...")

{ ..."name": "my_dictionary", ..."version": "1.0.0", ..."entities": [ ......{ ........."name": "entity_0", ........."value": "value_0" ......}, ......{ ........."name": "entity_1", ........."value": "value_1" ......} ...] }

public static string Stringify(Variant data, string indent = "", bool sortKeys = true, bool fullPrecision = false)

Parameters

data Variant
indent string
sortKeys bool
fullPrecision bool

Returns

string