Parsing JSON with UBot

Parsing JSON with UBot Studio

JSON is a standard format for data found in online applications and websites but unfortunately there is no built in support in UBot Studio 5.5 that allows you to work with it. In this tutorial we are going to use the built in functions and commands available in UBot to get data from JSON object and use it inside our bots.

The only component that is available by default in UBot and can parse JSON natively is JavaScript.  Another handy function is the $eval(), which can accept thinks like number operations and even a bit more complex JavaScript code. So in order to use JSON we are going to pass it through $eval(), and return the data we need. In this tutorial we are going to make a simple function that gets the value by its key using basic JavaScript.
This will be our test JSON string that we will use for the example.

{"url":"http:\/\/devmd.com\/","name":"Developer MD","meta":{"title":"Example of JSON in UBot Studio","description":"A tutorial on how to use JSON in UBot Studio 5.5 projects using the available functions and commands."}}

Since $eval(), needs an live and opened browser with a loaded page into it to function, we will need to make sure that there is one before we continue. In order to simplify this and make the code reusable we are going to create a new command called eval fix() that will make sure that the browser has a loaded page and $eval() will function properly. We will use this command before every $eval() call. Here is the UBot code:

define eval fix {
    set(#test,$eval("2+3"),"Local")
    if($comparison(#test,"=","")) {
        then {
            load html("An empty page was detected so eval() can not run. This is an auto generated HTML so that the function can be used!")
        }
    }
}

The first thing is to setup a test variable that will be the $eval() value of something we can test for, like a simple math operation of adding two numbers. If this operation fails $eval() will return an empty value that we can check and if such is the case load some HTML into the browser to jump start it.

JSON Parsing with JavaScript and UBot Studio

Now, when we have this function we can continue to the actual JSON parsing. The next function we are going to write will get the value from a JSON object by its key, the first parameter will be the JSON string and the second one the key we want to retrieve the value for:

define $json value(#json, #key) {
    eval fix()
    return($eval("try\{var json_63a4e94d5e86 = {#json}; json_63a4e94d5e86.{#key};\}catch(err)\{json_63a4e94d5e86 = ''; json_63a4e94d5e86;\}"))
}

Calling eval fix() ensures the proper execution of the code weather or not the browser is loaded with any HTML (like when using sockets). The next part is simple, we are creating a new JavaScript variable and giving it a random name that will not cause collisions with the already defined variables on any page. We then assign the JSON string as its value and retrieve the value by the key. The try and catch statements ensure that in case an invalid JSON string is provided, the remaining JavaScript code on the page would not be affected by the error.

Using this example as a guide, you can create any kind of functions that parse JSON data retrieved from services like Tumblr, Facebook and Twitter.

Download the code

If you wish to download the UBot source code for this example, simply sign up for our mailing list and the link to our downloads folder will be emailed to you upon confirmation. If you sill do not own Ubot, you can grab the latest version of UBot Studio here and start developing your own bots.