JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. HaasScript supports JSON natively, which means you can easily parse a JSON string into an object table using the ParseJson(string json) command.

Creating a JSON string

A JSON string is simply a string that contains data in JSON format. Here’s an example of a JSON string that represents a person:

{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}


This JSON string contains three properties: name, age, and city. The name property is a string, the age property is a number, and the city property is also a string.

Parsing a JSON string

Once you have a JSON string, you can parse it into an object table using the ParseJson(string json) command. Here’s an example:

json = [[
{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}
]]

local person = ParseJson(json)

Log(person.name)  -- Output: John Doe
Log(person.age)   -- Output: 30
Log(person.city)  -- Output: New York

This code will output the following:

John Doe
30
New York

The ParseJson() function takes a JSON string as input and returns an object table that represents the JSON data. You can then access the properties of the object table using dot notation.

Conclusion

JSON is a useful format for exchanging data between different systems, and HaasScript makes it easy to work with JSON data. With the ParseJson() function, you can quickly parse a JSON string into an object table and use the data in your scripts.

Back to: HaasScript Fundamentals > Using Data