8
JSON API
IgorTimofeev edited this page 2022-01-03 20:40:10 +07:00

This library allows to convert Lua objects to JSON strings and vice versa

Contents
json.encode
json.decode

json.encode(mixed data): string result

Attempts to encode given data to JSON string. If some data content has unsupported type or if it recursively links to itself, then error will be thrown. Supported data types:

  • Table
  • String
  • Number
  • Boolean
  • Nil

Example:

json.encode({
  testString = "hello world",
  testTable = {
    testArray = [1, 2, 3]
    testNumber = 123,
    testBool = true,
    testNil = nil
  }
})

Result:

> "{
 \"testString\": \"hello world\",
  \"testTable\": {
   \"testArray\": [
	  1,
	  2,
	  3
    ],
    \"testNumber\": 123,
    \"testBool\": true,
    \"testNil\": null
  }
}"

json.decode(string data): mixed result

Attempts to interpret given data string as JSON object and convert it to Lua type

Example:

json.decode("{
  \"test\": 123,
  \"hello\": \"world\"
}")

Result:

> {
 test = 123,
 hello = "world"
}