Parsing JSON

Vera uses JSON all over the place - there must be a convenient way to parse/generate JSON.
Any advice on how to work with JSON from Luup?

Have you tried http://json.luaforge.net/ ?

I have had a need yet. The only gotcha is that it uses the [tt]ltn12[/tt] lib from LuaSocket that I ran into before with Vera and SMTP

Might be worth giving it a go

I’m really curious what Vera uses to parse JSON stuff.
When I send an action with JSON params, Vera understands it very well, so there must be something in place.

While I was writing the air4vera application I read the html source code on the vera server. You can
get it yourself quite easily. The code used the “eval” statement in javascript to dig out various parameters
desired for the function it was performing. Since Adobe Air considers eval a security risk, I had to
pick the config file apart and reconstruct it in an array which was easy to parse. If you want to look at
the source code for that, its available.

Frank

My goal is not to hack into Vera’s config, I hoped to use the parser (I assumed it exists) Vera uses - to read my JSON (or XML) stuff. If they do ‘manual’ parsing with hardcoded tags and parameters it makes things very boring…

@325xi, as Vera is not written in Lua entirely, I don’t think the internal parser would be of any help to you…

Try http://jsonlint.com/ or http://json.parser.online.fr/ to parse the JSON files.

Don’t really see how could I use them to parse JSON response in the plugin…

@325xi, I downloaded the Lua JSON Library (referenced at the top of the thread) and the [tt]json.lua[/tt] file it includes for “raw” [tt]encode()/decode()[/tt] looks clean, without the Socket dependancies. It’s only once you start using it’s [tt]rpc.lua[/tt] and [tt]rpcserver.lua[/tt] that you’d hit the Lua Socket (and [tt]ltn12[/tt]) depends.

Have you given this Lib a try?

No, I didn’t. How do you add a new library in Vera? All I need from it is to feed it a JSON string, and get some elements…

@325xi, in the current version you can only “manually” move the Library into one of the pre-defined Lua directories.

You’ll see the default search path listed in my posting here:

http://forum.micasaverde.com/index.php?topic=1512.msg7935#msg7935

Unfortunately “[tt]./[/tt]” doesn’t resolve to where you’d want it, and the “[tt]/etc/cmh-ludl/[/tt]” directory (where the Device and other Vera Lua Browser-based uploads go" isn’t listed in the Lua Path.

I recommend you open a “For Aaron” case if you want the longer term Library support, and probably tack an extra comment onto the Bottom of the Mantis that MCV filed in that thread.

In the meantime, you can copy/inline the functions so you have the same logic. That’ll ease cutover down the road when the stuff gets implemented.

Did this ever get resolved? I’m looking to send/receive json from luup code. there doesn’t seem to be anything applicable in /usr/lib/lua/*
Thanks.

Yes, I was able to figure it out but it took me a while. I was using Adobe Air as the applications platform therefore the
request statement was written as an Air statement. I am not sure it will help. I have not used this in so long, I am not
even sure this is the final code I used.

/*
this is an example of getting data from vera json file. Must be run as an air appl. to work
*/
Ext.onReady(function(){
var loading_local_data_attempts=0;
var jsonData=0;
var jsonOriginalData=0;
window.callMe = document.getElementById(‘vera_data’).contentWindow.childSandboxBridge.callMe;
}) // endo of onReady
// var json_url = “http://172.16.16.128:3451/user_data?output_format=json”;
//the above file can be read by the evalJSON method the one below cannot

var json_url = "http://172.16.16.128:3451/data_request?id=alldata&output_format=json";

function is_object( mixed_var ){
if(mixed_var instanceof Array) {
return false;
} else {
return (mixed_var !== null) && (typeof( mixed_var ) == ‘object’);
}
}

function load_local_data(){

	//	var currentTime = new Date();
	//	var ctime = currentTime.getTime();
	//	log_message("Loading local data: "+json_url+'&rand='+ctime);
	// this did not work because of security sandbox voilations - will try sandbox bridge next
	var newURL = new air.URLRequest('http://172.16.16.128:3451/data_request?id=alldata&output_format=json');
	var loader = new air.URLLoader();
	loader.dataFormat = air.URLLoaderDataFormat.TEXT;
	loader.addEventListener(air.Event.COMPLETE, loadComplete);
	loader.load(newURL);
	function loadComplete(e){
		var myTextData = loader.dataFormat;
		alert('loader.data before the eval statement= ' + myTextData);
		
		var sec_name = eval("(" + myTextData +")");
		alert('sec_name');
	}
}

/*
// attempt to use the sandbox

Thanks @chairman_bubba for sharing your client side air code.
What I was looking for however was how do I consume or produce json
on the lua/luup side (from within a scene for example).

I’ve downloaded json4lua-0.9.50, and so I have the json.lua
package, but where is the best place to install it?

If I try out the lua statement:

local json=require('json')

I see the following in the log:

module 'json' not found:
	no field package.preload['json']
	no file './json.lua'
	no file '/usr/share/lua/json.lua'
	no file '/usr/share/lua/json/init.lua'
	no file '/usr/lib/lua/json.lua'
	no file '/usr/lib/lua/json/init.lua'
	no file './json.so'
	no file '/usr/lib/lua/json.so'
	no file '/usr/lib/lua/loadall.so'

I realize that I can put the json.lua file in any of these places and lua
will find it, but are any of those directories preserved after an upgrade?

Thanks,
–Jim

Jim,
Sorry I can’t help you here.
Frank

I placed it in /usr/lib/lua. Did not bother to worry if its kept after an upgrade, if its gone i’ll place it back ;D