Contact Us

CATEGORIES

Whoops…Nothing found

Try other keywords in your search

JSON Handling

 1 Minute

 0 Likes

 8 Views

When used correctly, working with JSON as a data exchange format represents one of the most efficient and reliable workflows available. This article describes the recommended best practices for implementing and structuring JSON-based data exchange.


What Is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format used to represent structured data. It is designed to be both human-readable and machine-parsable, making it a widely adopted standard for configuration files, data exchange, and API communication.

Purpose

JSON is commonly used to:

  • Transmit data between a client and a server (e.g., via REST APIs)
  • Define configuration parameters
  • Structure complex data such as objects, lists, and nested elements
  • Serialize and deserialize data between systems

Structure

JSON represents data using two primary structures:

  • Objects — Unordered collections of key–value pairs
  • Arrays — Ordered lists of values

Example:

{
  "timelineName": "Main Timeline",
  "duration": 120,
  "layers": [
    {
      "name": "Video Layer 1",
      "opacity": 1.0
    }
  ]
}

Data Types

JSON supports the following data types:

  • String
  • Number
  • Boolean (true / false)
  • Null
  • Object
  • Array

Key Characteristics

  • Uses UTF-8 encoding
  • Language-independent (supported by most programming languages)
  • Strict syntax rules (e.g., double quotes required for strings and keys)
  • Hierarchical and nestable structure

Workflow in Control

A JSON parsing library is included in Control. To use it, it can be imported using the require function.

if self.json == nil then
	self.json = require "json"
end

Decode

The decode function converts the data contained in a JSON string into a native Lua data structure, allowing the information to be accessed and processed programmatically.

Use it with:

self.json.decode

Example

This example uses a hard coded JSON string and decode it to create a Lua data Structure for us to use

-- helper import for dump funciton
if self.helper == nil then
	require "pixcHelper"
	self.helper = createPixcHelper(pixc,self())
end

-- json import for decode encode
if self.json == nil then
	self.json = require "json"
end

local string = '{"timelineName": "Main Timeline","duration": 120,"layers": [{"name": "Video Layer 1","opacity": 1.0}]}'

pixc.log(string)
local decodedString = self.json.decode(string)
pixc.log(self.helper.dump(decodedString))
-- decodedString is now lua data structure, the dump functions shows you in the log how to access certain elements
pixc.log(decodedString.layers[1].name) -- this is how to access the layer name as example

Log print:


Encode

To encode data, the encode function can be used. This converts a native Lua data structure into a JSON-formatted string suitable for transmission or storage.

Use it with:

self.json.encode()

Example

-- helper import for dump funciton
if self.helper == nil then
	require "pixcHelper"
	self.helper = createPixcHelper(pixc,self())
end


-- example data:
local user = {
    id = 1001,
    username = "john_doe",
    email = "john@example.com",
    isActive = true,
    age = 29,
    balance = 153.75,
    address = {
        street = "123 Main St",
        city = "New York",
        state = "NY",
        zip = "10001"
    },
    roles = { "admin", "editor", "user" },
    preferences = {
        theme = "dark",
        notifications = {
            email = true,
            sms = false,
            push = true
        }
    },
    lastLogin = nil  -- will typically become null in JSON
}

-- load library
if self.json == nil then
	self.json = require "json"
end

-- before encode
pixc.log(self.helper.dump(user))

-- encode data
local JsonString = self.json.encode(user)

-- after encoding
pixc.log(JsonString)

Log print:


Example Module

JsonExample


PIXERA 26.1  | 2. March 2026 | J.B.

Was this article helpful?