Alpha Video Training
Results 1 to 16 of 16

Thread: How to extract this flavor of json from rest api call ?

  1. #1
    Member cptutrecht's Avatar
    Real Name
    Ger Kurvers
    Join Date
    Feb 2010
    Location
    Netherlands, Utrecht
    Posts
    521

    Default How to extract this flavor of json from rest api call ?

    Hi, who can help me with this riddle:

    From a succesfull rest api call I get this response:

    ?varResponse
    = body = {"value":[{"id":"970396fe-534f-4a68-a3ac-cb2a98ab389a","IsArchived":false,"AdditionalName":null,"ChamberOfCommerceCity":null,"ChamberOfCommerceNumber":null,"City":null,"EMail":"info@buchhalter.nl","Name":"Buchhalter.ln","SearchName":"Buchhalter.ln","IdentifierNumber":1}]}
    error_code = 0
    error_text = ""
    headers = HTTP/1.1 200 OK
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Type: application/json; charset=utf-8
    Content-Language: nl-NL
    Expires: -1
    X-Content-Type-Options: nosniff
    X-Frame-Options: deny
    Date: Wed, 13 Jan 2016 21:49:31 GMT
    Connection: close
    Content-Length: 272
    +parsed_headers.
    I can't find a json method which extracts the json content form varResponse.body
    And sending back the content without adjustments to control result in the browser (using: e._set.result.value = response.body) throws a javascript error.......

    thanks in advance

    Ger
    Ger Kurvers
    Alpha Anywhere / V4.5.1 - Build 4642_4981 12-Oct-2017
    Development: Mysql, windows 10 64 Applicationserver: standard on Windows server 2012 R2

  2. #2
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: How to extract this flavor of json from rest api call ?

    i do not know dutch to interpret the company you are reaching for your api and what they supply,
    this result shown seems not just json response. it is contained in the entire response generated by the server.
    your call to the server should be reevaluated to limit to single response end point or the header should include the api call is json not general html as it appears, i could be wrong.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  3. #3
    Member cptutrecht's Avatar
    Real Name
    Ger Kurvers
    Join Date
    Feb 2010
    Location
    Netherlands, Utrecht
    Posts
    521

    Default Re: How to extract this flavor of json from rest api call ?

    Hi Gandhi,

    Thanks for looking in to it. Some clarification. varResponse is a pointer filled with a http_fetch request to the rest-api, so:

    dim varRespons as P
    varResponse = http_fetch(settings)

    What I showed was the content of varResponse in the interactive window of the code editor.
    The request is ok, but where i'm stuk is getting the (json?) data from varResponse.body which seems to contain:

    {"value":[{"id":"970396fe-534f-4a68-a3ac-cb2a98ab389a","IsArchived":false,"AdditionalName":null,"ChamberOfCommerceCity":null,"ChamberOfCommerceNumber":null,"City":null,"EMail":"info@buchhalter.nl","Name":"Buchhalter.ln","SearchName":"Buchhalter.ln","IdentifierNumber":1}]}
    Makes this the puzzle clearer?

    Thanks,

    Ger
    Ger Kurvers
    Alpha Anywhere / V4.5.1 - Build 4642_4981 12-Oct-2017
    Development: Mysql, windows 10 64 Applicationserver: standard on Windows server 2012 R2

  4. #4
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: How to extract this flavor of json from rest api call ?

    i understand. do it in the i.e. or safari or chrome and then,
    what i will do is inspect this in web inspector or f12 and check the response
    then assign the varResponse to one that gives json.
    because http_fetch(settings) is showing the http not json.
    or look into your http_fetch see if you can set the header to data/json.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  5. #5
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: How to extract this flavor of json from rest api call ?

    would please post your api call statement , you can remove username, password if any.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  6. #6
    Member cptutrecht's Avatar
    Real Name
    Ger Kurvers
    Join Date
    Feb 2010
    Location
    Netherlands, Utrecht
    Posts
    521

    Default Re: How to extract this flavor of json from rest api call ?

    Quote Originally Posted by GGandhi View Post
    would please post your api call statement , you can remove username, password if any.
    Yes of course, any help appriciated!
    It is a testaccount, so i left the username/password (will change it tomorrow :-)


    dim varHeader as C

    varHeader = "Accept: application/json" + crlf() + "Authorization: Basic UkxaQVBJOnNtdXJmMTI=" + crlf() + "Accept-Charset: iso-8859-1" + crlf() + "Accept-Encoding: gzip,deflate" + crlf() + "Accept-Language: nl-NL"

    dim pQuery as P
    pQuery.administration = ""
    pQuery.body = ""


    dim varQuery as C = ""


    dim settings as p
    settings.header = varHeader
    settings.host = "portal.reeleezee.nl"
    settings.method = "GET"
    settings.body = pQuery.body
    settings.page = "/API/v1/Customers"
    settings.query_string = varQuery
    settings.SSL_ON = .T.
    settings.ssl_ValidateCert = .F.

    dim varResponse as p = http_fetch(settings)
    Ger Kurvers
    Alpha Anywhere / V4.5.1 - Build 4642_4981 12-Oct-2017
    Development: Mysql, windows 10 64 Applicationserver: standard on Windows server 2012 R2

  7. #7
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: How to extract this flavor of json from rest api call ?

    okay, the call seems to be correct using http_fetch, i think the answer could be in alpha.

    you are using http_fetch() alpha function ( i use javascript scripts on mine)
    the pointer variable does contain all the info you posted.
    i would add
    Code:
    dim desired_value as c
    desired_value = varResponse.body
    then
    you could use json_parse to parse it out.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  8. #8
    Member cptutrecht's Avatar
    Real Name
    Ger Kurvers
    Join Date
    Feb 2010
    Location
    Netherlands, Utrecht
    Posts
    521

    Default Re: How to extract this flavor of json from rest api call ?

    Thanks for checking!

    I have tried that before and just now again. Unfotunately no succes. But I see that right now I get a different response, looks like hex value pairs, which alpha recognizes as Blob. So no chance that json_parse will work.

    I Will check with the api provider and see wether they are working at their api and messed things up at the moment :-(

    Quote Originally Posted by GGandhi View Post
    okay, the call seems to be correct using http_fetch, i think the answer could be in alpha.

    you are using http_fetch() alpha function ( i use javascript scripts on mine)
    the pointer variable does contain all the info you posted.
    i would add
    Code:
    dim desired_value as c
    desired_value = varResponse.body
    then
    you could use json_parse to parse it out.
    Ger Kurvers
    Alpha Anywhere / V4.5.1 - Build 4642_4981 12-Oct-2017
    Development: Mysql, windows 10 64 Applicationserver: standard on Windows server 2012 R2

  9. #9
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: How to extract this flavor of json from rest api call ?

    I tested with your credentials and it is returning hex representation.
    however using the data you had earlier in interactive window

    Code:
    dim jsP as p
    
    jsP = {"value":[{"id":"970396fe-534f-4a68-a3ac-cb2a98ab389a","IsArchived":false,"AdditionalName":null,"ChamberOfCommerceCity":null,"ChamberOfCommerceNumber":null,"City":null,"EMail":"info@buchhalter.nl","Name":"Buchhalter.ln","SearchName":"Buchhalter.ln","IdentifierNumber":1}]}
    ?jsP.value
    +[1].
    
    ?jsp.value[1].id
    = "970396fe-534f-4a68-a3ac-cb2a98ab389a"
    ?jsp.value[1].IsArchived
    = .F.
    so I would add
    dim desired_result as p
    desired_result = varResponse.body.value
    then parse that one.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  10. #10
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: How to extract this flavor of json from rest api call ?

    Code:
    ?varResponse
    = body = 0000 : 1f 8b 08 00 00 00 00 00 04 00 74 8e b1 6e c3 30 
    0010 : 0c 44 ff 85 b3 55 b4 71 9a 58 9e 9a 1a 1d 3c d4 
    0020 : 19 32 06 19 28 89 82 09 d0 32 a0 58 01 0a c3 ff 
    0030 : 1e 15 45 da 2e 1e 79 8f e4 bd 19 6e 28 89 a0 3e 
    0040 : cf c0 0e 6a d0 fb e7 52 ef 3c a9 d7 72 eb d5 16 
    0050 : 77 95 c2 12 ad b2 66 83 ba 42 53 56 1a a1 80 f6 
    0060 : 7a 88 b6 e7 1b e5 1b 8f 72 a5 02 0e ce f1 c4 63 
    0070 : 40 e9 70 c8 1f 43 12 29 a0 e9 71 30 14 8f be 19 
    0080 : 87 81 a2 a5 86 a7 af 55 d8 a5 ef f9 17 ff 5b fd 
    0090 : f8 44 96 ec c7 c1 8f 6f 26 d9 be 47 99 28 3e 05 
    00a0 : c9 36 3f 85 f0 fe 17 4b c8 f1 89 30 4b ae c0 d6 
    00b0 : 51 98 d8 33 c5 47 e9 cb 72 59 ee 03 00 bb d9 0a 
    00c0 : 99 10 01 00 00 
    error_code = 0
    error_text = ""
    headers = HTTP/1.1 200 OK
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Type: application/json; charset=utf-8
    Content-Encoding: gzip
    Content-Language: nl-NL
    Expires: -1
    Vary: Accept-Encoding
    X-Content-Type-Options: nosniff
    X-Frame-Options: deny
    Date: Thu, 14 Jan 2016 17:16:35 GMT
    Connection: close
    Content-Length: 197
    +parsed_headers.
    
    ?varResponse.body
    = 0000 : 1f 8b 08 00 00 00 00 00 04 00 74 8e b1 6e c3 30 
    0010 : 0c 44 ff 85 b3 55 b4 71 9a 58 9e 9a 1a 1d 3c d4 
    0020 : 19 32 06 19 28 89 82 09 d0 32 a0 58 01 0a c3 ff 
    0030 : 1e 15 45 da 2e 1e 79 8f e4 bd 19 6e 28 89 a0 3e 
    0040 : cf c0 0e 6a d0 fb e7 52 ef 3c a9 d7 72 eb d5 16 
    0050 : 77 95 c2 12 ad b2 66 83 ba 42 53 56 1a a1 80 f6 
    0060 : 7a 88 b6 e7 1b e5 1b 8f 72 a5 02 0e ce f1 c4 63 
    0070 : 40 e9 70 c8 1f 43 12 29 a0 e9 71 30 14 8f be 19 
    0080 : 87 81 a2 a5 86 a7 af 55 d8 a5 ef f9 17 ff 5b fd 
    0090 : f8 44 96 ec c7 c1 8f 6f 26 d9 be 47 99 28 3e 05 
    00a0 : c9 36 3f 85 f0 fe 17 4b c8 f1 89 30 4b ae c0 d6 
    00b0 : 51 98 d8 33 c5 47 e9 cb 72 59 ee 03 00 bb d9 0a 
    00c0 : 99 10 01 00 00 
    
    dim jsP as p
    jsP = varResponse.body
    ?jsP.value[1].id
    = "970396fe-534f-4a68-a3ac-cb2a98ab389a"
    I ran your code again, it does give hex but I assigned it to an object and the value extracted.
    if it is in an ux, you can assign that value to any control.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  11. #11
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,545

    Default Re: How to extract this flavor of json from rest api call ?

    Ger,

    The code you posted for the api is different than the code you used to produce the results from your first post. The code you posted encodes the response body. The code you originally ran does not.

    If you run the code below, the reponse body will match what you first posted. Using json_parse() on the response body will create a dot variable array which you can then get at.

    Code:
    dim varHeader as C 
    
    'varHeader = "Accept: application/json" + crlf() + "Authorization: Basic UkxaQVBJOnNtdXJmMTI=" + crlf() + "Accept-Charset: iso-8859-1" + crlf() + "Accept-Encoding: gzip,deflate" + crlf() + "Accept-Language: nl-NL"
    
    varHeader = "Accept: application/json" + crlf() + "Authorization: Basic UkxaQVBJOnNtdXJmMTI="
    
    dim pQuery as P
    pQuery.administration = ""
    pQuery.body = ""
    
    
    dim varQuery as C = ""
    
    
    dim settings as p
    settings.header = varHeader
    settings.host = "portal.reeleezee.nl"
    settings.method = "GET"
    settings.body = pQuery.body
    settings.page = "/API/v1/Customers"
    settings.query_string = varQuery
    settings.SSL_ON = .T.
    settings.ssl_ValidateCert = .F.
    
    dim varResponse as p = http_fetch(settings)	
    
    dim resp as p = json_parse(varResponse.body)
    Now, resp.value[1].EMail has the value info@buchhalter.nl, and resp.value[1].id has the value 970396fe-534f-4a68-a3ac-cb2a98ab389a.

    Some of the returned items are null and therefore do not get returned in the dot variable. E.g. "AdditionalName":null,"ChamberOfCommerceCity":null,"ChamberOfCommerceNumber":null,"City":null,"

    So these items will not show up in the dot variable. If you're looking for these values you'd need to test the dot variable for a value and that will confirm a returned null value.

    https://s3-us-west-2.amazonaws.com/a...sc/apiResp.PNG
    Last edited by Davidk; 01-14-2016 at 01:33 PM.

  12. #12
    Member cptutrecht's Avatar
    Real Name
    Ger Kurvers
    Join Date
    Feb 2010
    Location
    Netherlands, Utrecht
    Posts
    521

    Default Re: How to extract this flavor of json from rest api call ?

    Hi Gandhi, David,

    Both thanks for your effort. Gandhi for keeping me on the right track, I was getting desperate and your findings were really reassuring that I was on the right track.

    David, you discovered the real culprit: I indeed added the header "Accept-Encoding: gzip,deflate" (because that is requested by the api provider).

    This has as effect that without additional adjustment:
    the statements like

    dim jsP as p
    jsp = json_parse(varResponse.body)

    dim varBody as c
    varBody = jsP.value[1].id

    throw an xbasic error:
    Error executing Ajax callback. Error executing Ajax callback function 'get_administrations ': command: varBody = jsP.value[1].EMail 'json_generate(jsP) Property not found jsP.value[] subelement not found.
    Also other methods for giving back the response to controls of the component fail, for example e._state.response_body.value = json_generate(jsP) result in an empty json string {} in the control

    It works for now without that header.
    But how to work with that header? What is the right function to decode varResponse.body before putting it into an xbasic pointer or using it in xbasic functions?
    Ger Kurvers
    Alpha Anywhere / V4.5.1 - Build 4642_4981 12-Oct-2017
    Development: Mysql, windows 10 64 Applicationserver: standard on Windows server 2012 R2

  13. #13
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,545

    Default Re: How to extract this flavor of json from rest api call ?

    But if you're using the encoding header stuff then you're getting back binary data in the body. The body is binary. So... based on the api provider... what is it? They would tell you what is coming back. If you know what's coming back then it can, most likely, be dealt with.

    One thing for sure, you're not getting back JSON when using that encoding header... and therefore you cannot parse it.

  14. #14
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: How to extract this flavor of json from rest api call ?

    you are using http_fetch.
    regardless of what the provider provides the http_fetch will return a pointer variable. nothing to do with the provider.
    the pointer variable contains the body which is a character which is in json format and you can parse that to an object to get dot variables.
    when you use the gzip, deflate, the http_fetch is some how converted to low level http_get, by alpha, and the returns the binary data, the same data in binary format. again nothing to do with the provider it is the alpha thing.
    in short
    you can use all the stuff in the header except for gzip, deflate encoding.
    and parse the varResponse.body
    and the value inside can be found by various dot variables in the parsed value[1] dot notation
    for example
    Code:
    dim jsp as p
    jsp = json_parse(varResponse.body)
    dim customer_id as c
    customer_id = jsp.value[1].id
    now you can set value of any control with that value.
    if you must use gzip, deflate encoding ( i did not see that as a requirement in english translation of that site or did not see it in proper place ) then you would get the same data same mechanism to get the dot variables, the limitation is you need to know all the stuff contained in the value to get at it. or look in the browser they will translate the binary data to human readable format, i am sure.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  15. #15
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: How to extract this flavor of json from rest api call ?

    here is an implementation using your code minus gzip, deflate

    http://screencast.com/t/jFqPZvpFs
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  16. #16
    Member cptutrecht's Avatar
    Real Name
    Ger Kurvers
    Join Date
    Feb 2010
    Location
    Netherlands, Utrecht
    Posts
    521

    Default Re: How to extract this flavor of json from rest api call ?

    Thanks Gandhi,

    I got it also working using the methods you say.

    You wonder why there is only 1 record returned. That is because it is my test database with only 1 customer record in it.
    Just before this posting i added another one :-)

    Thanks again for all your help!

    Ger
    Ger Kurvers
    Alpha Anywhere / V4.5.1 - Build 4642_4981 12-Oct-2017
    Development: Mysql, windows 10 64 Applicationserver: standard on Windows server 2012 R2

Similar Threads

  1. cURL and Rest API Question for Newsfeed
    By ssmoore1 in forum Mobile & Browser Applications
    Replies: 1
    Last Post: 10-26-2014, 08:08 AM
  2. VS6 or V10 patch I see only 1 flavor ?
    By Clipper87 in forum Application Server Version 10 - Web/Browser Applications
    Replies: 17
    Last Post: 09-09-2010, 06:26 PM
  3. Flavor of Basic reco
    By emathias in forum Alpha Five Version 8
    Replies: 6
    Last Post: 09-22-2007, 06:06 PM
  4. Mouse Rest
    By Phil Rolf in forum Alpha Five Version 4
    Replies: 4
    Last Post: 11-18-2000, 04:51 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •