Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    control1 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    16

    Dealing with json

    Hi All,



    With the help of my provider, I have managed to get an API call running on my Access Database. When I run the code, it returns some data as XML/JSON and stores it as a string.

    I can display it in a message box with the result like so:
    Click image for larger version. 

Name:	result.png 
Views:	32 
Size:	4.8 KB 
ID:	28990


    I've been trying to figure out how I just get the actual useful data (credit) to be displayed - "2114.0000" and lose all the bumpf around it - but I'm struggling.

    Code:
    Public Sub getCredit()    Dim result As String
         Dim uid As String
         uid = "uid=eventcontrol"
         Dim pass As String
         pass = "&pass=HIDDEN"
         Dim format As String
         format = "&format=json"
         
         Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
         Request.Open "POST", "https://www.voodoosms.com/vapi/server/getCredit?" + uid + pass + format
         Request.Send
         
         result = Request.ResponseText
     
         MsgBox (result)
    
    
    End Sub
    Really appreciate a push in the right direction

    Cheers,
    Leigh

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    use the data import wizard to import the xml file to a temporary table.

    Or if this is always just one value, you can find it with string functions - something like

    it looks like your result is 2 lines so you need to go to the 2nd line

    dim strA() as string
    strA=split(result,chr(10)) ' check what the linefeed character is, might be something other than chr(10)
    if unbound(strA)=1 then iif(instr(strA(1),"<Credit>")=0,0,val(mid(strA(1), instr(strA(1)"<Credit>")+8))

  3. #3
    control1 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    16
    Quote Originally Posted by Ajax View Post
    use the data import wizard to import the xml file to a temporary table.

    Or if this is always just one value, you can find it with string functions - something like

    it looks like your result is 2 lines so you need to go to the 2nd line

    dim strA() as string
    strA=split(result,chr(10)) ' check what the linefeed character is, might be something other than chr(10)
    if unbound(strA)=1 then iif(instr(strA(1),"<Credit>")=0,0,val(mid(strA(1), instr(strA(1)"<Credit>")+8))
    Thanks for your reply and help Ajax. I'm still fairly new to VBA - and I'm struggling to understand/get to work that code.

    I basically only want to return what's inside the <credit></credit> tags.

    Cheers,
    Leigh

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    I basically only want to return what's inside the <credit></credit> tags.
    your post is not clear about what you want do - return to where? a control? as a function? to a variable to be used elsewhere? Also, your example appears to only return one value. If it returns more you will require a different solution. And you have not clarified what the linefeed character is, which will affect the split function if it is not chr(10)


    Assuming a single value returned and linefeed is chr(10) then add this code between the 'result = Request.ResponseText' and 'MsgBox' lines

    Code:
    dim strA() as string
     strA=split(result,chr(10)) ' check what the linefeed character is, might be something other than chr(10)
     if unbound(strA)=1 then msgbox iif(instr(strA(1),"<Credit>")=0,0,val(mid(strA(1),  instr(strA(1)"<Credit>")+8)))
    you should at least see what is being returned

    to determine the linefeed character, put this bit of code between the 'result = Request.ResponseText' and 'MsgBox' lines

    Code:
    dim I as integer
    for I=1 to len(result)
        debug.print mid(result,i,1) asc(mid(result,i,1))
    next i
    then inspect in the immediate window - you will be looking for ? followed by > and require the next character(s) until you see < followed by x, then m etc
    Last edited by CJ_London; 06-05-2017 at 10:52 AM. Reason: missed a bracket at the end of the code

  5. #5
    control1 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    16
    Quote Originally Posted by Ajax View Post
    your post is not clear about what you want do - return to where? a control? as a function? to a variable to be used elsewhere? Also, your example appears to only return one value. If it returns more you will require a different solution. And you have not clarified what the linefeed character is, which will affect the split function if it is not chr(10)


    Assuming a single value returned and linefeed is chr(10) then add this code between the 'result = Request.ResponseText' and 'MsgBox' lines

    Code:
    dim strA() as string
     strA=split(result,chr(10)) ' check what the linefeed character is, might be something other than chr(10)
     if unbound(strA)=1 then msgbox iif(instr(strA(1),"<Credit>")=0,0,val(mid(strA(1),  instr(strA(1)"<Credit>")+8))
    you should at least see what is being returned

    to determine the linefeed character, put this bit of code between the 'result = Request.ResponseText' and 'MsgBox' lines

    Code:
    dim I as integer
    for I=1 to len(result)
        debug.print mid(result,i,1) asc(mid(result,i,1))
    next i
    then inspect in the immediate window - you will be looking for ? followed by > and require the next character(s) until you see < followed by x, then m etc
    Thanks again for your reply. Basically the "result" that is being returned via the API call will be a number, that I want to use as a string. So when a user clicks on a button in my form, the api recieves the data, and displays it to the user in the message box. This is working now, but with all of the <xml> coding around it. It will always just be one number.


    I've tried the code you suggested:
    Code:
    dim strA() as string strA=split(result,chr(10)) ' check what the linefeed character is, might be something other than chr(10) if unbound(strA)=1 then msgbox iif(instr(strA(1),"<Credit>")=0,0,val(mid(strA(1), instr(strA(1)"<Credit>")+8))
    but I just get an error:

    Click image for larger version. 

Name:	2017-06-05 17_55_54-Microsoft Visual Basic for Applications - Event Control Log1.png 
Views:	24 
Size:	5.3 KB 
ID:	28994

    Thanks in advance
    Leigh

  6. #6
    control1 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    16
    The output from the Immediate window was:
    Code:
    < 60 x 120 
    m 109 
    l 108 
    > 62 
    < 60 
    r 114 
    e 101 
    s 115 
    u 117 
    l 108 
    t 116 
    > 62 
    2 50 
    0 48 
    0 48 
      32 
    O 79 
    K 75 
    < 60 
    / 47 
    r 114 
    e 101 
    s 115 
    u 117 
    l 108 
    t 116 
    > 62 
    < 60 
    c 99 
    r 114 
    e 101 
    d 100 
    i 105 
    t 116 
    > 62 
    2 50 
    0 48 
    9 57 
    0 48 
    . 46 
    0 48 
    0 48 
    0 48 
    0 48 
    < 60 
    / 47 
    c 99 
    r 114 
    e 101 
    d 100 
    i 105 
    t 116 
    > 62 
    < 60 
    / 47 
    x 120 
    m 109 
    l 108 
    > 62 
    
    
     10

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Further to ajax's comments and questions, can you tell us in plain English what you are trying to do?
    No JSON, no jargon just plain English.

    Can you show us the url and some sample data?

    We often see posters who are trying to parse orders/Invoices consisting of several lines. Can you give some idea of the kind of response you'll receive, and what exactly from that response do you need to extract?

    There are JSON parsers for vba but I haven't found 1 to work consistently.

    Running your code gives

    Code:
      : result : "<?xml version="1.0" encoding="utf-8"?>
    <xml><result>400</result><resultText>BAD REQUEST</resultText></xml>
    " : String
    Good luck.

  8. #8
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    Seeing as you have a value for a string variable ("result") how about
    Code:
    Function TestEval()
    
    Dim result As String
    Dim startPos As Integer, endPos As Integer
    
    result = "<xml><result>200 OK</result><credit>2114.0000</credit></xml>"
    startPos = InStr(1, result, "<credit>", 2) + 8
    endPos = InStr(1, result, "</credit>", 2)
    MsgBox Mid(result, startPos, endPos - startPos - 1)
    
    End Function
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    1. re the result - this is just showing the 2nd line so does not give the linefeed characters - but if this is the full string then all you need to do is change strA(1) to result in this line - also not the spelling correction

    if ubound(strA)=1 then msgbox iif(instr(strA(1),"<Credit>")=0,0,val(mid(strA(1), instr(strA(1)"<Credit>")+8)))

    2. the code you have used is missing a bracket at the end - the error message would have told you you were missing a bracket - I corrected it in my post

  11. #11
    control1 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    16
    Hi All,
    Many thanks for your messages. I'm sorry - I wasn't trying to be awkward. As you can tell, I'm very much a beginner - and just trying to blag my way through VBA.

    In plain English, I was just trying to get the number that's returned when the request is returned to be displayed - without all of the other XML codings around it. The code that @micron has provided has achieved this. However, if the returned value drops into the hundreds, it will only be 7 digits long and not work.

    I just thought there may have been some code that would recognise anything inbetween two defined <>'s - so even if it was <message>hello world</message) that was returned - there would be something that could pull out just "hello world".

    Ajax, I'm sorry that I still can't get the code you have provided to work - I'm still getting error messages. I don't know what I'm looking at really - I've never worked with this type of VBA code before.


    Maybe I'm too much of a novice to be attempting this - but I thank you for your help

    Leigh

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    Ajax, I'm sorry that I still can't get the code you have provided to work - I'm still getting error messages
    That is akin to you ringing the garage and saying 'my car won't work and I can't figure it out'.

    suggest post the code you are actually using together with the error messages. They may not mean anything to you at this stage of your knowledge, but will to the people who are trying to help. Also confirm the code you used to generate the list of characters - either you did not use the code I provided

    Code:
    dim I as integer
    for I=1 to len(result)
        debug.print mid(result,i,1) asc(mid(result,i,1))
    next i
    or the result value you got from the response text was structured differently to that provided originally, or you didn't post the entire output.

  13. #13
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    Quote Originally Posted by control1 View Post
    Hi All,

    However, if the returned value drops into the hundreds, it will only be 7 digits long and not work.
    I just thought there may have been some code that would recognise anything inbetween two defined <>'s - so even if it was <message>hello world</message) that was returned - there would be something that could pull out just "hello world".
    Leigh
    This is exactly how it should work. However, it seems to need a correction.
    Mid(result, startPos, (endPos - startPos))
    not
    Mid(result, startPos, endPos - startPos - 1)

  14. #14
    control1 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    16
    @Ajax

    The code that I have used to generate the list of characters is:

    Code:
    Public Sub getCredit()
        Dim result As String
         Dim uid As String
         uid = "uid=eventcontrol"
         Dim pass As String
         pass = "&pass=hidden"
         Dim format As String
         format = "&format=json"
         
         Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
         Request.Open "POST", "https://www.voodoosms.com/vapi/server/getCredit?" + uid + pass + format
         Request.Send
         
         result = Request.ResponseText
    
    
    Dim I As Integer
    For I = 1 To Len(result)
        Debug.Print Mid(result, I, 1); Asc(Mid(result, I, 1))
    Next I
    
    
         MsgBox (result)
    
    
    
    
    End Sub
    Which returns (in full)
    Code:
    < 60 
    ? 63 
    x 120 
    m 109 
    l 108 
      32 
    v 118 
    e 101 
    r 114 
    s 115 
    i 105 
    o 111 
    n 110 
    = 61 
    " 34 
    1 49 
    . 46 
    0 48 
    " 34 
      32 
    e 101 
    n 110 
    c 99 
    o 111 
    d 100 
    i 105 
    n 110 
    g 103 
    = 61 
    " 34 
    u 117 
    t 116 
    f 102 
    - 45 
    8 56 
    " 34 
    ? 63 
    > 62 
    
    
     10 
    < 60 
    x 120 
    m 109 
    l 108 
    > 62 
    < 60 
    r 114 
    e 101 
    s 115 
    u 117 
    l 108 
    t 116 
    > 62 
    2 50 
    0 48 
    0 48 
      32 
    O 79 
    K 75 
    < 60 
    / 47 
    r 114 
    e 101 
    s 115 
    u 117 
    l 108 
    t 116 
    > 62 
    < 60 
    c 99 
    r 114 
    e 101 
    d 100 
    i 105 
    t 116 
    > 62 
    2 50 
    0 48 
    8 56 
    7 55 
    . 46 
    0 48 
    0 48 
    0 48 
    0 48 
    < 60 
    / 47 
    c 99 
    r 114 
    e 101 
    d 100 
    i 105 
    t 116 
    > 62 
    < 60 
    / 47 
    x 120 
    m 109 
    l 108 
    > 62 
    
    
     10
    The problem that I was having was with this line of code that you told me to try
    Code:
    if ubound(strA)=1 then msgbox iif(instr(strA(1),"<Credit>")=0,0,val(mid(strA(1), instr(strA(1)"<Credit>")+8)))


    Full code that I am entering:
    Code:
    Public Sub getCredit()
        Dim result As String
         Dim uid As String
         uid = "uid=eventcontrol"
         Dim pass As String
         pass = "&pass=hidden"
         Dim format As String
         format = "&format=json"
         
         Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
         Request.Open "POST", "https://www.voodoosms.com/vapi/server/getCredit?" + uid + pass + format
         Request.Send
         
         result = Request.ResponseText
    
    
    Dim strA() As String
     strA = Split(result, Chr(10)) ' check what the linefeed character is, might be something other than chr(10)
    if ubound(strA)=1 then msgbox iif(instr(strA(1),"<Credit>")=0,0,val(mid(strA(1), instr(strA(1)"<Credit>")+8)))
    
    
    
    
    
    
         MsgBox (result)
    
    
    
    
    End Sub
    Which returns a Syntax error.

    Many thanks,

    Leigh

  15. #15
    control1 is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2017
    Posts
    16
    Hi Micron,
    Many thanks for your help.

    So that the code that you provided does work - unfortunately I can't test to see if it still works once the returned value is hundreds rather than thousands, as it's returning the credit balance of my account - so I can't adjust it.

    There are other API responses that I could explore, which instead of being <credit></credit> there could be different things - for example:
    Code:
    Public Sub Send()
        Dim result As String
         Dim uid As String
         uid = "uid=eventcontrol"
         Dim pass As String
         pass = "&pass=hidden"
         Dim validity As String
         validity = "&validity=1"
         Dim orig As String
         orig = "&orig=Test"
         Dim msg As String
         msg = "&msg=MESSAGE"
         Dim dest As String
         dest = "&dest=44700000000"
         Dim format As String
         format = "&format=json"
         
         Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
         Request.Open "POST", "https://www.voodoosms.com/vapi/server/sendsms?" + uid + pass + orig + validity + msg + dest + format
         Request.Send
         
         
              result = Request.ResponseText
        
    
    
         MsgBox Request.ResponseText
    End Sub
    Returns:

    Click image for larger version. 

Name:	2017-06-06 16_42_20-Microsoft Access.png 
Views:	20 
Size:	4.6 KB 
ID:	29007

    If i knew a little more about the code - could I doctor it for example to provide me with the value inbetween <resultText>""</resulttext> ?


    I feel that I need to go some reading around it so I can try and understand what the code is that you are kindly creating for me - so I can do it myself! Is there a particular name for these references, that I could google?

    Best,
    Leigh
    Last edited by control1; 06-06-2017 at 09:48 AM. Reason: removing api pw

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Importing/Reading JSON to Populate Fields
    By wrayman in forum Import/Export Data
    Replies: 22
    Last Post: 09-17-2018, 10:28 AM
  2. Dealing with UK dates
    By AB1984 in forum Database Design
    Replies: 6
    Last Post: 03-19-2017, 04:42 PM
  3. System DSN (MS Access) to REST API / JSON
    By Hitesh in forum Queries
    Replies: 1
    Last Post: 03-15-2016, 11:37 AM
  4. Dealing with the # character
    By frankivalli in forum Access
    Replies: 2
    Last Post: 06-12-2014, 10:57 AM
  5. Dealing with Old Balance
    By Altaful Haq in forum Forms
    Replies: 2
    Last Post: 02-16-2014, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums