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

json encode


Jay Ess

Question

Hi I am trying to encode the bank holdiays for the year, I have managed to get a list of holidays from calendrific.com and get this as my return

[{'name':New Year's Day','date':2021-01-01','name':Good Friday','date':2021-04-02','name':Early May Bank Holiday','date':2021-05-03','name':Spring Bank Holiday','date':2021-05-31','name':Christmas Day','date':2021-12-25','name':Boxing Day','date':2021-12-26','name':Substitute Bank Holiday for Christmas Day','date':2021-12-27','name':Substitute Bank Holiday for Boxing Day','date':2021-12-28'}]

 

The way i have got is as follows:

http:request(string.format(address),{options={headers={Accept="application/json"},method='GET'},

        success = function(status)

            local data = json.decode(status.data)

            data = data.response.holidays

            for _,d in ipairs(data) do

                if cR == "" then

                    cR="[{'name':"..d.name.."','date':"..d.date.iso.."'"

                else

                    cR=cR ..",'name':".. d.name.."','date':"..d.date.iso.."'"

                end

                

                --print (d.name)

                --print(d.date.iso)

            end 

            

            cR = cR.."}]"

            --string.gsub(cR,"'",""")

            print (cR)

        end,

        error = function(error) print(error) end

    }

    )

 

my problem is that in order to json encode it properly, from what I understand, I need all the single quotes to be double quotes, but i don't know how to get the data as double quotes,

 

any help would be great.

 

Many thanks.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

I don't think you have a problem with the response (the example you posted is not correct, missing matching quotes ('))

So what we see is probably after your attempt at "recoding".

Please post the raw response, what you get from

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0
  • Inquirer
  • @jgab

    this is the raw data that i am getting, i was thinking of storing the data in a variable as a table so that i can then update this once a year and check for bank holidays, thank you for your help.

     

    {"meta":{"code":200},"response":{"holidays":[{"name":"New Year's Day","description":"New Year's Day marks the start of a new year in the Gregorian calendar, the official calendar used in the United Kingdom.","country":{"id":"gb","name":"United Kingdom"},"date":{"iso":"2021-01-01","datetime":{"year":2021,"month":1,"day":1}},"type":["National holiday"],"locations":"All","states":"All"},{"name":"Good Friday","description":"Good Friday is just before Easter Sunday and is a public holiday in the United Kingdom.","country":{"id":"gb","name":"United Kingdom"},"date":{"iso":"2021-04-02","datetime":{"year":2021,"month":4,"day":2}},"type":["National holiday"],"locations":"All","states":"All"},{"name":"Early May Bank Holiday","description":"May Day, or the Early May Bank holiday, is on the first Monday of May each year.","country":{"id":"gb","name":"United Kingdom"},"date":{"iso":"2021-05-03","datetime":{"year":2021,"month":5,"day":3}},"type":["National holiday"],"locations":"All","states":"All"},{"name":"Spring Bank Holiday","description":"The spring bank holiday in the United Kingdom is on the last Monday of May each year.","country":{"id":"gb","name":"United Kingdom"},"date":{"iso":"2021-05-31","datetime":{"year":2021,"month":5,"day":31}},"type":["National holiday"],"locations":"All","states":"All"},{"name":"Christmas Day","description":"Christmas Day is a holiday that traditionally celebrates Jesus Christ's birth. It is on December 25 in the United Kingdom.","country":{"id":"gb","name":"United Kingdom"},"date":{"iso":"2021-12-25","datetime":{"year":2021,"month":12,"day":25}},"type":["National holiday"],"locations":"All","states":"All"},{"name":"Boxing Day","description":"Boxing Day is a holiday in the United Kingdom that falls on December 26 each year.","country":{"id":"gb","name":"United Kingdom"},"date":{"iso":"2021-12-26","datetime":{"year":2021,"month":12,"day":26}},"type":["National holiday"],"locations":"All","states":"All"},{"name":"Substitute Bank Holiday for Christmas Day","description":"December 27 is a bank holiday in the United Kingdom only in some years, depending on which day Christmas Day or Boxing Day occur.","country":{"id":"gb","name":"United Kingdom"},"date":{"iso":"2021-12-27","datetime":{"year":2021,"month":12,"day":27}},"type":["National holiday"],"locations":"All","states":"All"},{"name":"Substitute Bank Holiday for Boxing Day","description":"December 28 is a bank holiday in the United Kingdom only in some years, depending on which day Christmas Day or Boxing Day occur.","country":{"id":"gb","name":"United Kingdom"},"date":{"iso":"2021-12-28","datetime":{"year":2021,"month":12,"day":28}},"type":["National holiday"],"locations":"All","states":"All"}]}}

    Link to comment
    Share on other sites

    • 0

    May I suggest.

    Please login or register to see this code.

    Ok, now you have a key-value table with {date=name,...}

    You can encode it to json and store it in a variable

    fibaro:setGlobal("X",(json.encode(cR)))

     

    You can retrieve the table and quickly check if it is a holiday

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @jgab

    Thank you for your amazing contribution, as always spot on and ever so helpful.

     

    If I wanted to add another field to the table, how would i do that?

    as an example say i wanted the description too?

     

    and if i may what does _,n refer to as notice sometimes we use that and sometimes _,k, where can I get more info on this point please?

    Link to comment
    Share on other sites

    • 0

    Please login or register to see this code.

    Please login or register to see this code.

    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...