Parsing 3rd Party JSON

Hi All,

I am having “fun” parsing some JSON and I am slowing navigating my way through the tree.
But I have hit a bit of a snag.
Each level is Xojo.Core.Dictionary.
For the third level down I see:

Row 1: Name =
Value = Contents
Row 2: Name = Count
Value = 24

The data I need to get to is in the “Contents”.
I have tried:

Level4 = Level3.Lookup(nil,"NotFound")


Level4 = Level3.Lookup("","NotFound")

The error I get back is:

TypeMismatchException "Expected Object but got Text"

Anyone know how I should navigate to that?


Make your lookup backup nil and check for nil to see if Level4 was found. The default value needs to be similar to the actual value you’re expecting, so if Level4 is a JSONItem (Object in the JSON) then the default value needs to be an object.

But also, you can’t lookup “” because there’s no key to be looking up.
Perhaps, share the JSON response and we can help you figure out how to parse it?

Sure Tim.
I just need to format it and make sure nothing important is being displayed.

If Level4 is a Dictionary, your Lookup must either return a Dictionary or nil, but you are returning text when not found.

I think it should be:

Level4 = Level3.Lookup( "Contents", nil )

But that’s without seeing the original JSON that Tim suggested.

Is there a limit on how many lines / characters I can post?
When I paste the JSON in and click on “Post a Reply” it does nothing.

Yes, there is a limit. Might be better to paste it into a file, then share that.

Here is the file:

I don’t see anything in the file that matches your description. Everything has a key, as it should.

Hmmmm…let me post a screen shot.

I’m with Tim H., a bit confused.

The JSON is formatted as:

Object with keys api_activity and rpm_stats.

api_activity is an array where each item is an object with account_id, client_id, client_type, display_name, and counts.

counts is an object where each key is a number and the value is an object with a single key, count.

rpm_stats is roughly the same format but with different keys.

Contents, blue and underlined probably means in the debugger, looking at the JSONItem, right? That won’t tell us a whole lot about the goal unfortunately.

From the JSON provided, what values are you looking to get a hold of?
When I interact with APIs I have found it easier to create classes that behave like the objects I’m expecting and parse the JSON into those objects. That way, it’s almost like using ActiveRecord for API interaction.

What are the values you are trying to get a hold of?

I am trying to get hold of the counts.
This is a rolling count of the each API interface usage on an hourly basis and we need to keep a daily history for trending.

You’re looking at the debugger’s display of the dictionary object itself (which coincidentally has a property named “Count”, which may be confusing you), not what’s in the dictionary (the JSON). Click on “Contents” to see what is in the dictionary. Blue and underlined indicates a link that you can click on.

Can you show the rest of your code?

Ah, OK. Thanks for clarifying that.
So would I loop through each one as a JSONItem?

Yes. Each item will be either a JSONItem or an array. Pull them out as Auto and check.

Edit: No, they will not be JSONItems. Each will either be a Dictionary or an Array of Dictionaries.

OK. Thanks Tim(s). :slight_smile:

And thanks to everyone else ~