Skip to content
IgorTimofeev edited this page Jan 3, 2022 · 8 revisions

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"
}
Clone this wiki locally