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 problem


Question

Posted

Hi,

 

I have decoded a response to json table and i can fetch almost all key except one which has a '-' in the key name.

Its "x-token" and i always get attempt to perform arithmetic on field 'x' (a nil value)

So the '-' is messing up the json key value.

 

Any idea what i can do?

 

10 answers to this question

Recommended Posts

  • 0
Posted
1 hour ago, 747 said:

Hi,

I have decoded a response to json table and i can fetch almost all key except one which has a '-' in the key name.

Its "x-token" and i always get attempt to perform arithmetic on field 'x' (a nil value)

So the '-' is messing up the json key value.

Any idea what i can do?

It's probably not a json problem...

Please login or register to see this code.

Can you share the code how you fetch the keys?

  • 0
  • Inquirer
  • Posted

    Hi jgab,

     

    This is the response:

    Please login or register to see this image.

    /monthly_2021_06/image.png.aebf4858b0a60e90106a3ebf3d701b2b.png" />

     

    And if i use 

    local data = json.decode(resp.data);

    local resp = data.object_result.roleName

    then resp is "User"

    but if i use 

    local resp = data.object_result.x-token

     

    then i get the error message.

     

    BR

    • 0
    Posted (edited)
    14 hours ago, 747 said:

    Hi jgab,

     

    This is the response:

    Please login or register to see this link.

     

    And if i use 

    local data = json.decode(resp.data);

    local resp = data.object_result.roleName

    then resp is "User"

    but if i use 

    local resp = data.object_result.x-token

     

    then i get the error message.

     

    BR

    You can’t use .x-token as x-token is not a valid Lua identifier.

    Use

    Please login or register to see this code.

    The same goes if you have keys in local language - like åäö...

    Edited by jgab
    • Thanks 1
    • 0
  • Inquirer
  • Posted

    Ahh ok. Thanks!

    Will try it.

     

    BR

    • 0
  • Inquirer
  • Posted (edited)

    Hi jgab and thanks for that solution!

    That part is working fine now but the next request returns a table with 2 keyvalues..

    Im not sure how to parse that but maybe you know:

    Please login or register to see this image.

    /monthly_2021_06/image.png.055768a6364d5273781e07b8bfc23bc3.png" />

     

    I can iterate using:

    for i = 1, #resp.object_result do
                  for k,v in pairs(resp.object_result[i]) do
                        print("Key: "..k.. " value "..v)
                  end
                end

     

    But if would like to get the value of "Power" i need to iterate first to get value of "code" and check that is is "Power", then in the next iteration i need save the value presented as "value" (0 in this case).

     

    Im not sure how to fetch the value directly when i find that the code is "Power"?

     

    BR

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    Edited by 747
    Please disregard the last screenshot. Seems i cant remove it..
    • 0
    Posted

    Please login or register to see this code.

     

    So, it's an array of items so you need to search for the code.

    • Thanks 1
    • 0
  • Inquirer
  • Posted

    Thanks alot jgab!

     

    BR

    • 0
  • Inquirer
  • Posted (edited)

    Hi again..

     

    I almost have it working now.

    Just need to control on and off on my poolheater but cant get that to work.

     

    The curl cmd that people seems to get working is the following;´:

    curl -s -H "Content-Type: application/json; charset=utf-8" \

    -H "x-token: $token" \

    -d '{"param":[{"device_code":"'$device'","protocol_code":"power","value":"'$1'"}]}' \

    -X POST $cloudurl/app/device/control.json

     


    And with lua i have tried with this json body:

    data = json.encode({
        ["device_code"] = tostring(devCode),
        ["protocol_code"] = "Power",
        ["value"] = "1"
    })

     

    Response is:

    Please login or register to see this image.

    /monthly_2021_06/image.png.aea91e65285ece526dce1e33c7d0e113.png" />

     

    I dont know how to translate the curl cmd to http request..

     

    BR

     

       
       
       
       
       
       
    Edited by 747
    • 0
    Posted

    Try

    Please login or register to see this code.

     

    • Thanks 1
    • 0
  • Inquirer
  • Posted

    Ahh, perfect!

     

    Tack!

     :)

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