JSON Building

Hi Guys,

Is there a JSon builder (similar to newHashMap)?

Currently I am just building Json by joining strings together.

If not, is there a String.Format alternative? Like we could do something like this:

var s = String.Format("The time is: {0}", now.toString);

That would make things a bit easier.

You can use gson probably

com.google.gson.Gson

If you are using JavaScript, JSON is just a string representation of an Object.
So you can build up the data structure using maps and arrays and such and then use JSON.stringify(obj)

I actually use JSON in JQuery on my own website to handle AJAX callbacks, it’s not a foreign language to me. I was simply asking if there is a way in standard rules files.

I’d love to use javascript, however I given to understand I cannot have javascript in rules files, is this not correct? I’d love to be able to use a JSON object in a rules file. How exactly would I accomplish this, I have just added the ‘JSon Tools library’ from the UI, can I just import this? And if so, what would be it’s path?

Right now I am finding rules files far more convenient, however I am open to change.

There really isn’t “standard” rules. There are several options (see the Automation category of openHAB add-ons), all equally standard in their own right. Rules DSL is but one option, and frankly one of the least optimal choices these days. If you are not great at programming, Blockly is simpler to understand and more capable.

Well, no you can’t have them in .rules files. But you can have them in .js files. If you used jRuby you can use .rb files. All the details are in the readme docs for the respective add-ons: e.g. JS Scripting. With the exception of Blockly, all rules languages support file based rules.

That appears to be a Blockly Block Library. It can only be used in Blockly which, being graphical, can only be used through the UI. But Blockly converts to JS Scripting behind the scenes and all that library does is make the JSON class available as coding blocks that can be used in Blockly.

In other words, if you were to use JS Scripting directly, there’s nothing to import. JSON is part of the base language.

But if you did have a third party node module you wanted to use in JS Scripting (e.g. openhab_rules_tools), that can be installed via npm and imported into your rules (UI or file based) using the usual require command.

All the rules languages except for Java Rule (files only) and Blockly (UI only) can be used either from the UI or from files. But the details for using files depends on the individual language.