How to get last element of a JSON object in VBA

mintz

Board Regular
Joined
Aug 5, 2015
Messages
129
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:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I have the similar use case but i need to get the 1st item.

You said sMsg = Item("checkpoints")(1)("message") is returning the second message.
So what if i want 1st message?
I have tried using (0), it's returning nothing... BLANKS!
Any suggestions?

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

to return the last message?

Regards,
 
Upvote 0
I have the similar use case but i need to get the 1st item.

You said sMsg = Item("checkpoints")(1)("message") is returning the second message.
So what if i want 1st message?
I have tried using (0), it's returning nothing... BLANKS!
Any suggestions?

Look at my earlier post:
Try this:
Code:
    Dim checkpointsColl As Collection
    Set checkpointsColl = dicTrack.item(1).item("checkpoints")
    Debug.Print checkpointsColl.Count
    Debug.Print [COLOR=#ff0000][B]checkpointsColl.item(1).item("message")  'first checkpoint[/B][/COLOR]
    Debug.Print checkpointsColl.item(checkpointsColl.Count).item("message") 'last checkpoint
 
Upvote 0
Hi Raoof,

As far as I can tell the file is shared but if you are having trouble see if you can download it from my new website.

If I said the site was "under construction" I would be exaggerating!

But the download link should work: http://www.rickxl.com
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top