Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

tnesheim

Question

Hi

I've been able to connect to the central Norwegian public transport information hub to get subway departure times. The response I get looks like this:

 

{"data":{"quay":{"estimatedCalls":[{"destinationDisplay":{"frontText":"Bergkrystallen via Storo"},"aimedArrivalTime":"2019-09-09T09:50:00+0200"},{"destinationDisplay":{"frontText":"Ringen via Tøyen"},"aimedArrivalTime":"2019-09-09T09:59:00+0200"},{"destinationDisplay":{"frontText":"Bergkrystallen via Storo"},"aimedArrivalTime":"2019-09-09T10:05:00+0200"},{"destinationDisplay":{"frontText":"Ringen via Tøyen"},"aimedArrivalTime":"2019-09-09T10:14:00+0200"}]}}}

 

It looks like a table(?), but how can I "read out" spesific information?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
1 hour ago, tnesheim said:

Hi

I've been able to connect to the central Norwegian public transport information hub to get subway departure times. The response I get looks like this:

 

 

Hi @tnesheim ,

 

the above is JSON table and here is how structure can be better shown for easier reading:

 

Please login or register to see this code.

 

I also provided short code that will print all destinations with aimed arrival time just as example how to read table structure. Above code assumes that received data is stored in local variable response. For complicated table data I use editors that highlight closing brackets like ZeroBrane Studio. Also Firefox can be used to show nicely JSON responses.

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 0
  • Inquirer
  • Thanks a lot. This will give me a good start to work further on. But to get there I need to overcome a few issues. The first is that the json.decode gives an error code saying that it get a table but expects a string. Also, it seems difficult to get the values of an variable outside the http:response function....reports NIL...

     

    --[[
    %% properties
    %% events
    %% globals
    --]]


    local payload=tostring('{quay(id:"NSR:Quay:10825"){estimatedCalls(timeRange: 72100, numberOfDepartures: 4){destinationDisplay{frontText}aimedArrivalTime}}}')
    local Url = 'https://api.entur.io/journey-planner/v2/graphql'
    local httpClient = net.HTTPClient();
    httpClient:request(Url, { 
           options = {
           checkCertificate = false,
           method="POST",
           headers={
                   ['content-type'] = 'application/graphql', 
                   ['ET-Client-Name'] = 'TNsmart-tavle', 
                   ['User-Agent'] = 'TNscript for Entur'
                   },
           data= payload
           },
               success = function (response) fibaro:debug(response.data) end,
               error  =  function (err) fibaro:debug ("Error: "  .. err) end
       })


              local jW=json.encode(response)
          local jW=response.data
          for i=1, #jW.data.quay.estimatedCalls do
          fibaro:debug(string.format("Destination No. %s, name: %s arrival time: %s",i,jW.data.quay.estimatedCalls.destinationDisplay.frontText,jW.data.quay.estimatedCalls.aimedArrivalTime))
          end

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I'we used a global variale to get the data out of the "http:request loop", and I have also simplified your code:

     

    local var=fibaro:getGlobalValue("Entur")
    local jW=json.decode(var)

    test= jW.data.quay.estimatedCalls[2].aimedArrivalTime
    fibaro:debug(test)

     

    Whatever I do I get a NIL-error returned in the third code line...

    Link to comment
    Share on other sites

    • 0

    Hi @tnesheim ,

     

    What is received under the data part fo the response table is not actually JSON but some kind of their format. Anyway, to get table it is needed to get rid of the character "\" from the string and then it can be finally decoded properly to the table. Here is code that I tested and is working:

     

    Please login or register to see this spoiler.

     

    Now you can finally get results of query and display them on for example virtual device.

     

    Enjoy coding :-) 

    Edited by Sankotronic
    • Thanks 1
    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...