Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: How to get last element of a JSON object in VBA
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Aug 2015
    Posts
    117
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default How to get last element of a JSON object in VBA

    Code:
    {
      "data": {
        "trackings": [
          {
            "tracking_number": "3A5V198427201",
            "shipment_pickup_date": "2016-10-31T23:49:00",
            "tag": "InTransit",
            "title": "3531",
            "checkpoints": [
              {
                "created_at": "2016-11-01T16:09:23+00:00",
                "message": "The electrolic infomation has been received ."
              },
              {
                "created_at": "2016-11-14T17:54:14+00:00",
                "message": "Shipment Ready For Transit."
              },
              {
                "created_at": "2016-11-14T17:54:14+00:00",
                "message": "Shipment Depart From Hub Scan."
              },
              {
                "created_at": "2016-11-15T06:38:28+00:00",
                "message": "A UPS shipping label has been created"
              }
            ]
          }
        ]
      }
    }
    This is how I get the first checkpoint message:
    sMsg = Item("checkpoints")(1)("message")

    How do I get the last one? ("A UPS shipping label has been created")
    Last edited by mintz; Nov 16th, 2016 at 11:21 AM.

  2. #2
    MrExcel MVP RickXL's Avatar
    Join Date
    Sep 2013
    Location
    UK North Midlands
    Posts
    4,314
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Hi,

    You don't say how you are creating your Json Item so I will have to guess.

    I have used JsonConverter in the past. Based on that, you might be able to replace the "1" index number with "Item.Count" (without the quotes).

    Regards,
    RickXL

    Excel 2013 and Windows 10

  3. #3
    Board Regular
    Join Date
    Aug 2015
    Posts
    117
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Quote Originally Posted by RickXL View Post
    Hi,

    You don't say how you are creating your Json Item so I will have to guess.

    I have used JsonConverter in the past. Based on that, you might be able to replace the "1" index number with "Item.Count" (without the quotes).

    Regards,
    HiRick

    That is a JSON response, I simply put the xmlHttp.response text in a string variable called JsonText

    JsonText = xmlHttp.responseText

    I have just tried:
    sMsg = Item("checkpoints")(Item.Count)
    but I get the "9 subscript out of range"
    Last edited by mintz; Nov 17th, 2016 at 04:38 PM.

  4. #4
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,857
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Quote Originally Posted by mintz View Post
    That is a JSON response, I simply put the xmlHttp.response text in a string variable called JsonText

    JsonText = xmlHttp.responseText
    But what are you doing with JsonText? You need to parse/convert it using a VBA JSON parser which puts it into a specific data structure which you can then access to extract the elements you need.

  5. #5
    Board Regular
    Join Date
    Aug 2015
    Posts
    117
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Quote Originally Posted by John_w View Post
    But what are you doing with JsonText? You need to parse/convert it using a VBA JSON parser which puts it into a specific data structure which you can then access to extract the elements you need.
    I'm parsing it like Rick said using the VBA JSON converter:

    Code:
    Set ParsedJSONDict = JsonConverter.ParseJson(JsonText)
    Set dicData = ParsedJSONDict("data")
    Set dicTrack = dicData("trackings")
    
        For Each Item In dicTrack
             sMsg = Item("checkpoints")( *last item* )("message") ' get last checkpoint message
    Last edited by mintz; Nov 17th, 2016 at 06:54 PM.

  6. #6
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,857
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Try this:
    Code:
        Dim checkpointsColl As Collection
        Set checkpointsColl = dicTrack.item(1).item("checkpoints")
        Debug.Print checkpointsColl.Count
        Debug.Print checkpointsColl.item(1).item("message")  'first checkpoint
        Debug.Print checkpointsColl.item(checkpointsColl.Count).item("message") 'last checkpoint
    NB - although it doesn't affect the above code, "trackings" is a Collection, therefore the dicTrack object is a Collection, not a Dictionary as your variable name suggests. Look in the Locals window at run-time and you will see that the last data type of dicTrack is Collection.
    Last edited by John_w; Nov 18th, 2016 at 12:23 PM.

  7. #7
    Board Regular chicagocomputerclasses's Avatar
    Join Date
    Mar 2015
    Location
    Chicago, IL
    Posts
    645
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Quote Originally Posted by mintz View Post
    JsonText = xmlHttp.responseText
    When you debug.print JsonText do you get the same json response text as the one you get in your browser?

  8. #8
    MrExcel MVP RickXL's Avatar
    Join Date
    Sep 2013
    Location
    UK North Midlands
    Posts
    4,314
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Could it be that sMsg = Item("checkpoints")(1)("message") is actually returning the second message and that you need to use:
    Code:
    sMsg = Item("checkpoints")(Item.Count - 1)
    

    to return the last message?

    Regards,
    RickXL

    Excel 2013 and Windows 10

  9. #9
    Board Regular
    Join Date
    Aug 2015
    Posts
    117
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Quote Originally Posted by John_w View Post
    Try this:
    Code:
        Dim checkpointsColl As Collection
        Set checkpointsColl = dicTrack.item(1).item("checkpoints")
        Debug.Print checkpointsColl.Count
        Debug.Print checkpointsColl.item(1).item("message")  'first checkpoint
        Debug.Print checkpointsColl.item(checkpointsColl.Count).item("message") 'last checkpoint
    NB - although it doesn't affect the above code, "trackings" is a Collection, therefore the dicTrack object is a Collection, not a Dictionary as your variable name suggests. Look in the Locals window at run-time and you will see that the last data type of dicTrack is Collection.
    That worked! I was able to access the last message using this:

    Code:
    For Each Item In dicTrack
      Set checkpointsColl = Item("checkpoints")
      sMsg = Item("checkpoints")(checkpointsColl.Count)("message")
      ' further processing
    Next Item
    PS How do I change "trackings" to a Collection instead of a Dictionary?
    Last edited by mintz; Nov 18th, 2016 at 05:13 PM.

  10. #10
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,857
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: How to get last element of a JSON object in VBA

    Quote Originally Posted by mintz View Post
    PS How do I change "trackings" to a Collection instead of a Dictionary?
    Code:
    Dim trackingsColl As Collection
    and replace instances of dicTrack with trackingsColl.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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