Contact Us

CATEGORIES

Whoops…Nothing found

Try other keywords in your search

Ingesting data via JSON

 0 Minutes

 0 Likes

 1721 Views

If you ever need to ingest external data via json here is a sample code.
This code has been provided by Benjamin Mueller from http://benni-m.de/

Notes from Benni:

A few things about using tables or json in Control / Lua Control. 
Lua has two data options to get table data or list objects. First is basicly just the name. e.g. 
{ "test": "Hello World"} 
If you converted this to the LUA script posted below you get access to the token "test" in two different ways:
jsondecoded["test"] or jsondecoded.test 

If you have a list that you like to use, the index starts with 1. e.g. 
{ "test": ["Hello", "World"]} jsondecoded["test"][1] or jsondecoded.test[1] for the word "Hello"
 and jsondecoded["test"][2] or jsondecoded.test[2] for the word "World" 

With this method you can get all the values you need. 
e.g. for more complex situation where you have normal values and lists of values: 
{ "test": [ {"message":"Hello"}, {"message":"World"} ] } jsondecoded["test"][1]["message"]
 or jsondecoded.test[1].message for "Hello" 
and jsondecoded["test"][2]["message"] 
or jsondecoded.test[2].message for "World" 

For more examples of Lua Tables: https://www.lua.org/pil/2.5.html

--Sample Code:

local json=require "json"
local open=io.open

local function read_file(path)
 local file = open(path, "rb")  --r read mode and b binary mode
 if not file then return nil end
 local content = file:read "*a"  --*a or *all reads the whole file
 file:close()
 return content
end

local fileContent = read_file("D:\\testfolder\\test.json")

local jsonDecoded = json.decode(fileContent)
pixc.log(jsonDecoded.track)
pixc.log(jsonDecoded.mettno)




 

Was this article helpful?