# llList2Json

*LSL function*

```lsl
string string llList2Json(string type, list values);
```

- `string type`: JSON target type (JSON_ARRAY or JSON_OBJECT).
- `list values`: List of values to serialize.

- Returns: `string`
- Energy: 10

This function takes a list and returns a [JSON](http://json.org) string of that list as either a json object or json array.

To convert a json formatted string into a list use [llJson2List](/functions/llJson2List/).

Returns a [string](https://wiki.secondlife.com/wiki/string) that is either **values** serialized as a JSON **type**, or if an error was encountered [JSON_INVALID](/constants/JSON_INVALID/).

```lsl title="How to use" frame="terminal"
string result = llList2Json("", []);
```

## Specification

See [Json_usage_in_LSL](https://wiki.secondlife.com/wiki/Json_usage_in_LSL)

- If type is [JSON_OBJECT](/constants/JSON_OBJECT/) the list must be a strided list of key, value pairs and a string representing a json object will be returned.
- If type is [JSON_ARRAY](/constants/JSON_ARRAY/) then a string representing a json array will be returned.
- If type is any other string then [JSON_INVALID](/constants/JSON_INVALID/) will be returned.
- Json types from the input list are inferred from the LSL data type
- [integer](https://wiki.secondlife.com/wiki/integer) and [float](https://wiki.secondlife.com/wiki/float) types are encoded as [JSON_NUMBER](/constants/JSON_NUMBER/)
- "true" and "false" are encoded as [JSON_TRUE](/constants/JSON_TRUE/) and [JSON_FALSE](/constants/JSON_FALSE/), respectively
- "null" is encoded as [JSON_NULL](/constants/JSON_NULL/)
- properly-formatted json strings in the input are kept as-is (will either be [JSON_STRING](/constants/JSON_STRING/), [JSON_OBJECT](/constants/JSON_OBJECT/), or [JSON_ARRAY](/constants/JSON_ARRAY/))
- all other strings encoded as [JSON_STRING](/constants/JSON_STRING/). Strings are trimmed of whitespace at the beginning and end, as if passed thru [llStringTrim](/functions/llStringTrim/)
- Note in particular that strings containing valid JSON numbers are converted to JSON strings, not left as JSON numbers ([BUG-6284](https://jira.secondlife.com/browse/BUG-6284)). This differs to the treatment that [llJsonSetValue](/functions/llJsonSetValue/) gives to strings that contain JSON numbers, which leaves them as numbers.

| Type Flags | Value | Unicode | [Integer](/functions/llOrd/) | [URL Encoded](https://wiki.secondlife.com/wiki/Category:LSL_Encoding/URL) | HTML Encoded | Description |
| --- | --- | --- | --- | --- | --- | --- |
| [JSON_INVALID](/constants/JSON_INVALID/) |  | U+FDDO | 64976 | "%EF%B7%90" | &amp;#xFDD0; | Value returned when inputs are not well formed. |
| [JSON_OBJECT](/constants/JSON_OBJECT/) |  | U+FDD1 | 64977 | "%EF%B7%91" | &amp;#xFDD1; |  |
| [JSON_ARRAY](/constants/JSON_ARRAY/) |  | U+FDD2 | 64978 | "%EF%B7%92" | &amp;#xFDD2; |  |

## Caveats

- Note that string **values** items are interpreted as JSON, not LSL strings. Quotation marks, if required, must be added explicitly. For example, `llJson2List(llList2Json(JSON_ARRAY, ["bacon", "true", "false", "null"]))` returns the LSL list `["bacon", JSON_TRUE, JSON_FALSE, JSON_NULL]`, while `llJson2List(llList2Json(JSON_ARRAY, ["\"bacon\"", "\"true\"", "\"false\"", "\"null\""]))` returns the LSL list `["bacon", "true", "false", "null"]`
- [llJson2List](/functions/llJson2List/) does not order the JSON alphabetically, like [llJsonSetValue](/functions/llJsonSetValue/) does!

```lsl
default
{
    touch_start(integer total_number)
    {
       string json_msg = llList2Json(JSON_OBJECT, ["C","3","B","2","A","1"]);
        llOwnerSay("llList2Json: " + json_msg); // Output: llList2Json: {"C":"3","B":"2","A":"1"}

        json_msg = llJsonSetValue(json_msg, ["D"], "4");
        llOwnerSay("llJsonSetValue: " + json_msg); // Output: llJsonSetValue: {"A":"1","B":"2","C":"3","D":4}
    }
}
```

## Examples

<details open>
<summary>Example 1</summary>

```lsl collapse={1-6}
default
{
    state_entry()
    {
        list items;
        string json;

        items = ["89556747-24cb-43ed-920b-47caed15465f"];
        json = llList2Json(JSON_ARRAY, items);
        // ["89556747-24cb-43ed-920b-47caed15465f"]

        items = ["pi", 3.14, "set", "[1,2,3]", "status", "ok"];
        json = llList2Json(JSON_OBJECT, items);
        // {"pi":3.140000,"set":[1,2,3],"status":"ok"}

        items = [0, 3.14, "[1,2,3]", "{}"];
        json = llList2Json(JSON_ARRAY, items);
        // [0,3.140000,[1,2,3],{}]
    }
}
```

</details>

<details>
<summary>Example 2</summary>

```lsl
string CSV2Json(string csv)
{
    list li = llCSV2List(csv);
    return llList2Json(JSON_ARRAY, li);
}
// This function converts a comma separated values string to a Json array string.
// CSV strings are often used for link-messages, notecards and they are easier to type as input commands.
// A Json-Array can store multiple of those as a nested list within the same Json-string via JSON_APPEND.
```

</details>

- You could store multiple lists that set different particle effects in the same string. Use LlList2Json() to write the string and `llJson2List()` to read the particle system defining lists from it. The loss in float-accuracy (Json has BAD float-to-string-conversion) does not matter as much for particle effects.

- You could store multiple lists, that set different `llSetPrimitiveparamsFast()` values for different situation (like different animation key frames), in the same Json String.

## History

Date of Release [20/05/2013](https://wiki.secondlife.com/wiki/Release_Notes/Second_Life_Server/13#13-05-20-276191)

## See also

### Functions

- [llJson2List](/functions/llJson2List/)
- [llJsonGetValue](/functions/llJsonGetValue/)
- [llJsonSetValue](/functions/llJsonSetValue/)
- [llJsonValueType](/functions/llJsonValueType/)

### Articles

- [Typecast](https://wiki.secondlife.com/wiki/Typecast)

---

*Source: [LlList2Json](https://wiki.secondlife.com/wiki/LlList2Json) on the Second Life Wiki. Content from the Second Life Wiki articles LlList2Json (revision 1218964, 2026-07-25) and Template:LSL Constants/JSON (revision 1215249, 2023-10-29), CC BY-SA 3.0.*

---

From lsl.dev: https://lsl.dev/functions/llList2Json/
