Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website

How to get JSON data via API?


  • Hello,

    I will collect data via API for our project. I would like to get the readValue of each device but I couldn't find the way to get it. Because I am very new at javascript and API environment.

    The Data API format is shown below. In order to get data, I have tried 2 different methods:

    Method 1:
    I have added HTTP retriever datasource and added a point. Then I get an error.

    Is there any suggestion, what is the best way/datasource to get this data?
    How do we enter authorization info (basic, bearer etc.) to http datasources?

    HTTP retriever url: http://10.35.173.145:84/api/Lin
    Point Value RegEx: .readValue":(\d+).(?=."deviceName":"PDU-24A-42F11".).
    Error: Invalid response from "http://10.35.173.145:84/api/Lin": 401

    //url: http://10.35.173.145:84/api/Lin
    // Bearer Auth code: eyJhbGciOiJIUzI1NiIsInR5
    {
        "lastUpdate": "2020-06-10T10:52:39.8352606+03:00",
        "result": [
            {
                "deviceId": 27,
                "deviceName": "PDU-24A-42F11",
                "room": "DC_KAR",
                "row": "M_KAR",
                "location": "SOL",
                "tagId": 20987,
                "tagName": "Voltage",
                "unit": "V",
                "readDate": "2020-06-10T10:51:45",
                "readValue": 228.87
            },
            {
                "deviceId": 33,
                "deviceName": "PDU-23B-7F1",
                "room": "DC_KAR",
                "row": "M_KAR",
                "location": "SOL",
                "tagId": 21155,
                "tagName": "Voltage",
                "unit": "V",
                "readDate": "2020-06-10T10:52:23",
                "readValue": 228.97
            },
            {
                "deviceId": 34,
                "deviceName": "PDU-23B-29F11",
                "room": "DC_KAR",
                "row": "M_KAR",
                "location": "SOL",
                "tagId": 21183,
                "tagName": "Voltage",
                "unit": "V",
                "readDate": "2020-06-10T10:51:31",
                "readValue": 228.97
            },
            {
                "deviceId": 35,
                "deviceName": "PDU-23B-25F11",
                "room": "DC_KAR",
                "row": "M_KAR",
                "location": "SOL",
                "tagId": 21211,
                "tagName": "Voltage",
                "unit": "V",
                "readDate": "2020-06-10T10:51:00",
                "readValue": 228.87
            },
            {
                "deviceId": 42,
                "deviceName": "PDU-23B-33F11",
                "room": "DC_KAR",
                "row": "M_KAR",
                "location": "SOL",
                "tagId": 21327,
                "tagName": "Voltage",
                "unit": "V",
                "readDate": "2020-06-10T10:51:34",
                "readValue": 228.97
            },
            {
                "deviceId": 30,
                "deviceName": "PDU-23B-38F11",
                "room": "DC_KAR",
                "row": "M_KAR",
                "location": "SOL",
                "tagId": 21071,
                "tagName": "Voltage",
                "unit": "V",
                "readDate": "2020-06-10T10:52:30",
                "readValue": 228.89999
            }
    }
    

    Method 2:
    I have added a global script shown below. When I print the content, I see the data. But I cannot get the read value of device. How can I get it?

    function linye(message) {
      
         HttpBuilder.request({
        	path: "http://10.35.173.145:84/api/Lin",
        	method: "GET",
        	headers: {
        	    ContentType : "application/json",
                Authorization : "Bearer eyJhbGciOiJIUzI1NiIsInR5",
        	},
    
        	err: function(status, headers, content) { //errorCallback for linguistic completion
        		throw "Request got bad response: " + status+content;
        	},
        	resp: function(status, headers, content) { //responseCallback
        	    
        		return true; //will print in wrapping print()
        	},
        	excp: function(exception) { //exceptionCallback
        		throw exception.getMessage();
        	}
        })
    }
    

    Thanks in advance!


  • I'll help you out Nurr, let's simplify things a bit shall we?

    METHOD 1

    1. Complete method 2 below
    2. Create a virtual datasource to host the data, make sure each point created is Settable and set the XIDs to be the same as the devicenames if they are all different. Need them all to be unique.
    3. create a scripting datasource or a meta point datasource.
    4. Use the script in method 2 to make the http request to get your data.
    5. Attach to the script context point, all of the points you made in your virtual datasource.
    6. In your callback use:
    var foxesCBHandler = function(status, headers, content)
    {
       var data = JSON.parse(content);
       data = data.result; //lets get the info we want.
        for (var pnt in CONTEXT_POINTS) //list of points in context
        {
            for(var i=0; i<data.length; i++) //loop through available values.
            {
                if(this[pnt].xid === data[ i ].deviceName) //items match
                {
                    this[pnt].set(data[ i ].readValue, Date.parse(data[ i ].readDate) ); //set value and time
                    break; //finished, next context pt!
                 }
            }
        }
    }
    
    path: "http://10.35.173.145:84/api/Lin",
        	method: "GET",
        	headers: {
        	    ContentType : "application/json",
                Authorization : "Bearer eyJhbGciOiJIUzI1NiIsInR5",
        	}
    

    Becomes

    httpReq("http://10.35.173.145:84/api/Lin", "get", foxesCBHandler,  {
        	    ContentType : "application/json",
                Authorization : "Bearer eyJhbGciOiJIUzI1NiIsInR5"
        	});
    
    1. set your script to run as often as you require ie every 15 mins.

    METHOD 2
    Create a global script called HTTP REQUESTS.
    Inside I wrote this wrapper to handle all http related requests from all of my scripts/meta data scripts.

    function httpReq(url,method,callback,headers,params)
    {//Second format, full request
        if(headers===undefined){headers={};}
        if(params===undefined){params={};}
        if(method=="get")
        {
        HttpBuilder.get(url,headers,params)
    	.err(function(status, headers, content) { //setErrorCallback for linguistic completion
    		  //setErrorCallback for linguistic completion
                	print(status);	print(content);
    	}).resp( 
    	 callback
    
     ).excp(function(exception) { //setExceptionCallback
    		throw exception.getMessage();
    	}).execute();
        }
        else
        {
             HttpBuilder.post(url,headers,params)
    	.err(function(status, headers, content) { //setErrorCallback for linguistic completion
    		  //setErrorCallback for linguistic completion
                	print(status);	print(content);
    	}).resp( 
    	 callback
    	
    	 ).excp(function(exception) { //setExceptionCallback
    		throw exception.getMessage();
    	}).execute();
        }
    }
    

    This simplifies things because now all you need to do is write your callback function to handle your response:

    var handleResponse = function(status, headers, content){  /*play with content */};
    

    Then make your request inside the same script with:

    var url = "https://api.domain.com";
    var method = "get";  //Or "post"
    var headers = {ContentType : "application/json",};
    var parameters ={}; /* If needed exclude if not*/
    httpReq(url, method, handleResponse , headers);
    
    /*httpReq(url, method, handleResponse , headers, parameters);*/
    
    /*can just do  httpReq("https://api.domain.com" ,"get",handleResponse,{ContentType : "application/json",Authorization : "Bearer eyJhbGciOiJIUzI1NiIsInR5"}) */
    

    This makes handling your http callbacks a bit more easily and just makes your scripts that little bit tidier...

    Fox


  • @MattFox Thank you for your time and solution. I did what you have said and it works !!


  • Excellent news, always happy to help

    Fox