Announcement

Collapse
No announcement yet.

Help available?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help available?

    Hi,
    How do I get the following information as a table in AMS?

    Code:
    {
        "checks": [
            {
                "hostname": "example.com",
                "id": 85975,
                "lasterrortime": 1297446423,
                "lastresponsetime": 355,
                "lasttesttime": 1300977363,
                "name": "My check 1",
                "resolution": 1,
                "status": "up",
                "type": "http"
            },
            {
                "hostname": "mydomain.com",
                "id": 161748,
                "lasterrortime": 1299194968,
                "lastresponsetime": 1141,
                "lasttesttime": 1300977268,
                "name": "My check 2",
                "resolution": 5,
                "status": "up",
                "type": "ping"
            },
            {
                "hostname": "example.net",
                "id": 208655,
                "lasterrortime": 1300527997,
                "lastresponsetime": 800,
                "lasttesttime": 1300977337,
                "name": "My check 3",
                "resolution": 1,
                "status": "down",
                "type": "http"
            }
        ]
    }

    I use the following code:
    Code:
    	local ENV = [[]]
    	local targetURL = "https://api.pingdom.com/api/2.0/checks/";
    	local Soap = luacom.CreateObject("WinHttp.WinHttpRequest.5.1");
    	if (Soap ~= nil) then 
    		Soap:Open("GET", targetURL, 0) 
    	    Soap:SetRequestHeader("Authorization", "Basic "..cAuth)
    	    Soap:SetRequestHeader("Host", cHost)
    	    Soap:SetRequestHeader("App-Key", cAppKey)
    	
    	    Soap:Send(); 
    	 
            local result = Soap.ResponseText;
    
    		if (Soap.Status == 200) then
    			result = String.Replace(result, "\\/", "/", false);
    			result = String.Replace(result, "\\r\\n", "\r\n", false);
    			result = String.Replace(result, "\\n", "\n", false);
    			
    		    json = require("dkjson");
    			local tblResult, pos, err = json.decode(result, 1, nil);
    			if err then
        			Dialog.Message("Error", err);
    			else
        			for k, v in pairs(tblResult) do
            			Dialog.Message("Table Item", k.."="..v);
        			end
    			end
    		
    		else
    			Dialog.Message("Error:", result, MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    		end
    	
    		Soap = nil;
    	end
    I would like to get the list with messages as a table, so I can query it, but I don't know how to do this?

    Can someone help me out or point me in the right direction?

    Thanks!

    Eric

  • #2
    Originally posted by Friethoe View Post
    Hi,
    How do I get the following information as a table in AMS?

    Code:
    {
        "checks": [
            {
                "hostname": "example.com",
                "id": 85975,
                "lasterrortime": 1297446423,
                "lastresponsetime": 355,
                "lasttesttime": 1300977363,
                "name": "My check 1",
                "resolution": 1,
                "status": "up",
                "type": "http"
            },
            {
                "hostname": "mydomain.com",
                "id": 161748,
                "lasterrortime": 1299194968,
                "lastresponsetime": 1141,
                "lasttesttime": 1300977268,
                "name": "My check 2",
                "resolution": 5,
                "status": "up",
                "type": "ping"
            },
            {
                "hostname": "example.net",
                "id": 208655,
                "lasterrortime": 1300527997,
                "lastresponsetime": 800,
                "lasttesttime": 1300977337,
                "name": "My check 3",
                "resolution": 1,
                "status": "down",
                "type": "http"
            }
        ]
    }

    I use the following code:
    Code:
    	local ENV = [[]]
    	local targetURL = "https://api.pingdom.com/api/2.0/checks/";
    	local Soap = luacom.CreateObject("WinHttp.WinHttpRequest.5.1");
    	if (Soap ~= nil) then 
    		Soap:Open("GET", targetURL, 0) 
    	    Soap:SetRequestHeader("Authorization", "Basic "..cAuth)
    	    Soap:SetRequestHeader("Host", cHost)
    	    Soap:SetRequestHeader("App-Key", cAppKey)
    	
    	    Soap:Send(); 
    	 
            local result = Soap.ResponseText;
    
    		if (Soap.Status == 200) then
    			result = String.Replace(result, "\\/", "/", false);
    			result = String.Replace(result, "\\r\\n", "\r\n", false);
    			result = String.Replace(result, "\\n", "\n", false);
    			
    		    json = require("dkjson");
    			local tblResult, pos, err = json.decode(result, 1, nil);
    			if err then
        			Dialog.Message("Error", err);
    			else
        			for k, v in pairs(tblResult) do
            			Dialog.Message("Table Item", k.."="..v);
        			end
    			end
    		
    		else
    			Dialog.Message("Error:", result, MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
    		end
    	
    		Soap = nil;
    	end
    I would like to get the list with messages as a table, so I can query it, but I don't know how to do this?

    Can someone help me out or point me in the right direction?

    Thanks!

    Eric
    the place where your looking throw your returns and putting them in a dialong this is where you fill in your table out side the look create a table

    lTable ={};

    then in loop build the table up.

    I only just woken up and mess up tables myself but if you look around you might find some basic table apps .
    Plugins or Sources MokoX
    BunnyHop Here

    Comment


    • #3
      If you use the David Kolf's JSON module, then you already have all data in a table after calling json.decode(), unless the operation failed - there is nothing else you need to do.
      You need to know, of course, how the parsed data is organized in the table, but a simple debug printout can show you how to access the information.

      Here is a sample based on the data you posted:

      Code:
      local sData = [[{
          "checks": [
              {
                  "hostname": "example.com",
                  "id": 85975,
                  "lasterrortime": 1297446423,
                  "lastresponsetime": 355,
                  "lasttesttime": 1300977363,
                  "name": "My check 1",
                  "resolution": 1,
                  "status": "up",
                  "type": "http"
              },
              {
                  "hostname": "mydomain.com",
                  "id": 161748,
                  "lasterrortime": 1299194968,
                  "lastresponsetime": 1141,
                  "lasttesttime": 1300977268,
                  "name": "My check 2",
                  "resolution": 5,
                  "status": "up",
                  "type": "ping"
              },
              {
                  "hostname": "example.net",
                  "id": 208655,
                  "lasterrortime": 1300527997,
                  "lastresponsetime": 800,
                  "lasttesttime": 1300977337,
                  "name": "My check 3",
                  "resolution": 1,
                  "status": "down",
                  "type": "http"
              }
          ]
      }]];
      
      
      function PrintData(key, value, parent)
        if (type(value) == "table") then
          -- "value" is a table, so walk this table recursively
          for i, j in pairs(value) do
            if (parent ~= "") then
              PrintData(i, j, parent.."["..key.."]");
            else
              PrintData(i, j, key);
            end
          end
        else
          -- "value" is not a table, so simply print the key = value pair
          if (parent ~= "") then
            if (type(value) == "boolean") then
              if value then
                Debug.Print(parent .. "[" .. key .. "]=true\r\n");
              else
                Debug.Print(parent .. "[" .. key .. "]=false\r\n");
              end
            else
              Debug.Print(parent .. "[" .. key .. "]=".. value.."\r\n");
            end
          else
            Debug.Print(key .. "=".. value.."\r\n");
          end
        end
      end
      
      
      -- parse JSON data using David Kolf's JSON module
      json = require("dkjson");
      local tDecoded, nPos, sErr = json.decode(sData, 1, nil);
      if sErr then
        Dialog.Message("Error", sErr);
      else
        -- print all data recursively
        Debug.ShowWindow();
        for k, v in pairs(tDecoded) do
          PrintData(k, v, "tDecoded");
        end
      end
      
      -- show how to access a single item in the table
      Debug.Print("\r\n***TEST***\r\n");
      Debug.Print("tDecoded[\"checks\"][3][\"lasterrortime\"]=".. tDecoded["checks"][3]["lasterrortime"].." (compare with above)\r\n");
      Running the code above will produce this output:

      Code:
      tDecoded[checks][1][type]=http
      tDecoded[checks][1][id]=85975
      tDecoded[checks][1][resolution]=1
      tDecoded[checks][1][lasterrortime]=1297446423
      tDecoded[checks][1][lasttesttime]=1300977363
      tDecoded[checks][1][name]=My check 1
      tDecoded[checks][1][lastresponsetime]=355
      tDecoded[checks][1][status]=up
      tDecoded[checks][1][hostname]=example.com
      tDecoded[checks][2][type]=ping
      tDecoded[checks][2][id]=161748
      tDecoded[checks][2][resolution]=5
      tDecoded[checks][2][lasterrortime]=1299194968
      tDecoded[checks][2][lasttesttime]=1300977268
      tDecoded[checks][2][name]=My check 2
      tDecoded[checks][2][lastresponsetime]=1141
      tDecoded[checks][2][status]=up
      tDecoded[checks][2][hostname]=mydomain.com
      tDecoded[checks][3][type]=http
      tDecoded[checks][3][id]=208655
      tDecoded[checks][3][resolution]=1
      tDecoded[checks][3][lasterrortime]=1300527997
      tDecoded[checks][3][lasttesttime]=1300977337
      tDecoded[checks][3][name]=My check 3
      tDecoded[checks][3][lastresponsetime]=800
      tDecoded[checks][3][status]=down
      tDecoded[checks][3][hostname]=example.net
      
      ***TEST***
      tDecoded["checks"][3]["lasterrortime"]=1300527997 (compare with above)
      Remember that you need to use quotes around strings when using them as indexes of associative arrays.

      Ulrich
      Attached Files
      Last edited by Ulrich; 07-11-2021, 09:48 AM.

      Comment


      • #4
        Hi Guys,

        Thank you very much for your input.

        @Ulrich: That clarified a lot to me and thanks for your example, awsome!!!

        Thank you all, very much appreciated!!

        Kind regards,
        Eric

        Comment

        Working...
        X