How to Read JSON Values Without Dictionaries/Interference?

For implementing a build monitor I perform an API request to a Jenkins server (URL call) that returns JSON. From this JSON only some values (for certain keys, like "result" and "number") are relevant.

My first attempt was to put the (JSON/text) result of the URL call in a dictionary and then get the relevant info. This works in isolation, but I want to have multiple monitors (for multiple repos/branches). Apparently that dictionary is shared by all instances of my subroutine, causing the wrong build status to be displayed.

How can I get specific JSON values without using dictionaries?

(Or better: in such a way that I can run multiple instances of my subroutine in parallel without interference?)

Have you looked at the features that KM provides to use JSON? A good place to look is here: action:Set JSON Value [Keyboard Maestro Wiki] which also has links to the other JASON features.

1 Like

If you have JSON in a variable, then you can use the JSONValue token to extract parts of the JSON.

1 Like

I have (briefly) tried the JSONValue token before posting, but failed.

Is putting the (JSON) text our Jenkins server returns in a variable sufficient to have JSON in a variable, or do I need to do something special to let KM know it’s JSON and not (any) plain text?

Additional question: suppose the JSON is stored in variable localJSON and I want to fetch data for several keys (currently, "result" and "number"), do I need to hardcode them all like this?

  • %JSONValue%localJSON.result%
  • %JSONValue%localJSON.number%

Or can I do this (and make localParameter and input parameter of my subroutine)?

  • * %JSONValue%localJSON.%Variable%localParameter%%

(an in-between solution is to use a switch, but then I still need to hardcode all keys I want to support)

I was expecting "SUCCESS" to be displayed, but instead I get an error on the second action.

image

What am I doing wrong?

Mine works so compare yours with mine:

Test JSON.kmmacros (2.6 KB)

KM 0 2021-11-16_17-41-11

BTW - I use local variables wherever possible to ensure they don't "stick around".

1 Like

I normally also use local variables, but wanted to keep the example simple.

Will try again later today.

1 Like

Imported @tiffle's example (thanks!). That indeed works for me too.

  1. I'm still curious what I did wrong in the function. Anyone?
  2. Also, (how) can I parameterise result instead of hardcode it?

EDIT: Answer to the second question: use { and }, as explained on the Wiki (JSON Paths).

image

1 Like

The image you originally posted had CALCULATION in one of the fields and you also had not tokenised JSONValue - that’s where you went wrong.

The image you originally posted had CALCULATION in one of the fields and you also had not tokenised JSONValue - that’s where you went wrong.

Are you sure?

I was trying to use the function JSONVALUE which has this line in the Wiki:

JSONVALUE(jsonpath) is more or less equivalent to CALCULATE(%JSONValue%jsonpath%).

So I'm still confused why your approach works and mine did not...

I agree with rob that something seems wrong when neither the JSONVALUE function nor the CALCULATE function seem to work as the wiki says they should. Tiffle is using the JSONValue token instead of these functions, which works. That's a workaround, but it's still an open question why neither of the functions work. A workaround is always good, but a workaround doesn't answer the question.

If I remember correctly – calculations produce only numbers.

This confused me when I was fooling with JSON not long ago.

-Chris

1 Like

A calculation can only return a number.

So JSONVALUE(J.number) should work and return 218.

But JSONVALUE(J.result) would return SUCCESS and SUCCESS is not a number, so that is an error.

Calculations only work on numbers.

3 Likes

Rats! I make that mistake so often! At least once a week. Sorry.

I did not know that. Good to know! This answers my (additional) question.

And since @tiffle's approach worked I already adopted that and now have a working build monitor :smiley:

build monitor

3 Likes