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.decode, error: expected value but found invalid token at character 1


Question

Posted

I am trying, to read out a json which came from some motor.

 

In the virtual device I can fetch the json and when I debug it looks quit normal -> json_incoming [{"Actief" : 45, "Stand": 1}]

but when I try to decode the json it just gave an error which I don't really understand: expected value but found invalid token at character 1

 

I attached the main loop and the debugwindow.

 

Does anybody know how to fix this error?

 

 

Many thanks!
 
 

 

 

 

 

 

 

Please login or register to see this attachment.

Please login or register to see this attachment.

9 answers to this question

Recommended Posts

  • 0
Posted

Don't know and unfortunatley I have a few things to do first, but I find this question intriguing enough to do a quick test:

 

Please login or register to see this code.

It does a round trip decode encode and it works. So it prints:

Please login or register to see this code.

Maybe your data contains extra, unprintable characters? Can you please print the length of the response string?
  • 0
  • Inquirer
  • Posted

    Thank you for your fast response.

     

    I put the incoming "response" string into the function string.len and it has a length of 28.

    • 0
    Posted

    That's odd, I count 29 in my example. So your response can't be identical. If it's whitespace it doesn't matter. Can you spot the difference? Or... Is my example wrong?

    • 0
  • Inquirer
  • Posted

    the diffrence is a space just before the colon, of "Actief".

    So in my view the string length is correct.

    • 0
    Posted

    OK then. I'm thinking aloud here... would it be possible to dump the ascii code of each character? I don't have code at hand but I can have a look at it later. And, what does print(type(X)) say (X is your json string or response variable)?

    • 0
  • Inquirer
  • Posted

    I find the solution and figured out what the problem was. There are two ways to solve this problem

    response=incoming json

    JsonTable=decoded json.

     

    1:

    JsonTable=json.decode(tostring(response)). # some how this translates the json to be interpretable for the json.decode function, probably the json.decode function doesn't understand UTF8

     

    2:

    JsonTable=json.decode(response) # In this case I could force the json encoding in the motor to be written as ASCII instead of UTF8. The ASCII JSON string doesn't cause any trouble with the json.decode() function.

     

    Anyway, thank you for your help, I appreciated that

    • 0
    Posted

    Glad it's solved! I'm unsure about the explanation, UTF-8 is the default encoding, but I don't think I've ever had a HC2 related JSON file that contained non-ascii data. So I don't know.

    Please login or register to see this image.

    /emoticons/default_icon_wink.gif" alt=";-)" />

    • 0
    Posted
    On ‎2016‎-‎04‎-‎12 at 3:09 PM, WillyBumBum said:

    I find the solution and figured out what the problem was. There are two ways to solve this problem

    response=incoming json

    JsonTable=decoded json.

     

    1:

    JsonTable=json.decode(tostring(response)). # some how this translates the json to be interpretable for the json.decode function, probably the json.decode function doesn't understand UTF8

     

    2:

    JsonTable=json.decode(response) # In this case I could force the json encoding in the motor to be written as ASCII instead of UTF8. The ASCII JSON string doesn't cause any trouble with the json.decode() function.

     

    Anyway, thank you for your help, I appreciated that

     

    I am having the same issue. Tried your recipe #1 above and it worked for a while but now I am back to square one again. 

     

    Has anyone else encountered this problem and found away to fix it?

     

    • 0
    Posted
    On 2018-03-05 at 7:21 PM, perjar said:

     

    I am having the same issue. Tried your recipe #1 above and it worked for a while but now I am back to square one again. 

     

    Has anyone else encountered this problem and found away to fix it?

     

     

    Update: It turned out that the problem I had was that the REST interface I was calling returned an error message which caused json.decode to malfunction. 

    Problem fixed by not calling json.decode if there is an error from the request.

     

     

     

     

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