Jump to content

[SOLVED] JSON string not decodable - (Moved)


JoeBar
 Share

Recommended Posts

Hi, i'm trying to implement Imgur API functions in an AutoIt program.

I successfully coded the oAuth2 Authentication and i'm testing some Imgur functions.

 

Example : https://api.imgur.com/3/account/me/images

The string received is like :

{"data":[{"id":"fd6f54s","title":null,"description":null,"datetime":1574502473,"type":"image\/png","animated":false,"width":1147,"height":715,"size":53516,"views":18,"bandwidth":963288,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":"ACCOUNT","account_id":ACCOUNTID,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"d6f5sd4fsf","name":"image","link":"https:\/\/i.imgur.com\/fd6f54s.png"}

I'm using JSON.au3 UDF and i don't manage to make it working :

Local $Obj = Json_Decode($hRequestSSL)
Local $Links = Json_Get($Obj, '["data"]["link"]')

The Json_Decode sends in console :

"E:\Portable\Dev\AutoIt3\IncludeP\JSON.au3" (374) : ==> Variable must be of type "Object".:
Return $Object.Keys()
Return $Object^ ERROR

It says it's not an object or i have seen in some UDF that Json_Decode takes a string as parameter.

I don't know what's wrong.

Edited by JoeBar
Link to comment
Share on other sites

14 minutes ago, JoeBar said:

I'm using JSON.au3 UDF and i don't manage to make it working

Can you be more specific on where you got the UDF because it seems we are using different versions and I'm seeing different results. For example --

#include <JSON.au3> ; https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn

$sResponse = '{"data":[{"id":"fd6f54s","title":null,"description":null,"datetime":1574502473,"type":"image\/png","animated":false,"width":1147,"height":715,"size":53516,"views":18,"bandwidth":963288,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":"ACCOUNT","account_id":ACCOUNTID,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"d6f5sd4fsf","name":"image","link":"https:\/\/i.imgur.com\/fd6f54s.png"}'
Json_Dump($sResponse)

give me this output --

+-> .data[0].id  =fd6f54s
+-> .data[0].title  =
+-> .data[0].description  =
+-> .data[0].datetime  =1574502473
+-> .data[0].type  =image/png
+-> .data[0].width  =1147
+-> .data[0].height  =715
+-> .data[0].size  =53516
+-> .data[0].views  =18
+-> .data[0].bandwidth  =963288
+-> .data[0].vote  =
+-> .data[0].nsfw  =
+-> .data[0].section  =
+-> .data[0].account_url  =ACCOUNT
+-> .data[0].account_id  =ACCOUNTID
+-> .data[0].edited  =0
+-> .data[0].deletehash  =d6f5sd4fsf
+-> .data[0].name  =image
+-> .data[0].link  =https://i.imgur.com/fd6f54s.png

and the following runs as expected for me --

#include <JSON.au3> ; https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn

$sResponse = '{"data":[{"id":"fd6f54s","title":null,"description":null,"datetime":1574502473,"type":"image\/png","animated":false,"width":1147,"height":715,"size":53516,"views":18,"bandwidth":963288,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":"ACCOUNT","account_id":ACCOUNTID,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"d6f5sd4fsf","name":"image","link":"https:\/\/i.imgur.com\/fd6f54s.png"}'
$Obj = Json_Decode($sResponse)
$Links = Json_Get($Obj, '["data"][0]["link"]')
ConsoleWrite($Links & @CRLF)
$Links = Json_Get($Obj, ".data[0].link")
ConsoleWrite($Links & @CRLF)

 

Link to comment
Share on other sites

I'm using this from 148114-a-non-strict-json-udf-jsmn

; ============================================================================================================================
; File      : Json.au3 (2018.12.29)
; Purpose   : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
; Author    : Ward
; Dependency: BinaryCall.au3
; Website   : http://www.json.org/index.html
;
; Source    : jsmn.c
; Author    : zserge
; Website   : http://zserge.com/jsmn.html
;
; Source    : json_string_encode.c, json_string_decode.c
; Author    : Ward
;             Jos - Added Json_Dump()
;             TheXMan - Json_ObjGetItems and some Json_Dump Fixes.
;             Jos - Changed Json_ObjGet() and Json_ObjExists() to allow for multilevel object in string.
; ============================================================================================================================

Edit : I don't know how but it's working now ...

Edited by JoeBar
Link to comment
Share on other sites

  • Developers

Woks fine when you use proper keys ;)  Json_Dump will lead the way.

#include <json.au3>
$hRequestSSL = '{"data":[{"id":"fd6f54s","title":null,"description":null,"datetime":1574502473,"type":"image\/png","animated":false,"width":1147,"height":715,"size":53516,"views":18,"bandwidth":963288,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":"ACCOUNT","account_id":ACCOUNTID,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"d6f5sd4fsf","name":"image","link":"https:\/\/i.imgur.com\/fd6f54s.png"}'
Json_Dump($hRequestSSL)
Local $Obj = Json_Decode($hRequestSSL)
Local $Links = json_get($obj, '.data[0].link')
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Links = ' & $Links & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

3 minutes ago, Jos said:

Woks fine when you use proper keys ;)  Json_Dump will lead the way.

#include <json.au3>
$hRequestSSL = '{"data":[{"id":"fd6f54s","title":null,"description":null,"datetime":1574502473,"type":"image\/png","animated":false,"width":1147,"height":715,"size":53516,"views":18,"bandwidth":963288,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":"ACCOUNT","account_id":ACCOUNTID,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"d6f5sd4fsf","name":"image","link":"https:\/\/i.imgur.com\/fd6f54s.png"}'
Json_Dump($hRequestSSL)
Local $Obj = Json_Decode($hRequestSSL)
Local $Links = json_get($obj, '.data[0].link')
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Links = ' & $Links & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

Jos

Thanks, i badly formatted the Json_Get param 🙄

Link to comment
Share on other sites

  • Developers

Moved to the appropriate forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers
5 minutes ago, JoeBar said:

Thanks, i badly formatted the Json_Get param 🙄

Yes and also posted in the wrong forum! so maybe a little more checking before doing?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

1 hour ago, Jos said:

Yes and also posted in the wrong forum! so maybe a little more checking before doing?

I immediately reported my thread to be moved, but it has not been done ...

Edited by JoeBar
Link to comment
Share on other sites

  • Developers
3 hours ago, JoeBar said:

I immediately reported my thread to be moved, but it has not been done ...

Of course it is, I did it when I posted that last remark as you can see in the post above that as that is auto-magically generated when we do. ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

8 minutes ago, Jos said:

Of course it is, I did it when I posted that last remark as you can see in the post above that as that is auto-magically generated when we do. ;) 

I was talking that it had not been done before your last post.

If i could have done it, i would.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...