Help with parsing JSON data

When fetching a URL, I get JSON data returned in a variable CoinRAW as follows:

[{"subver":"/Coin:1.0.3/","protocol":609,"nodes":["174.138.5.166","128.85.5.248"]},{"subver":"/Coin:1.0.4/","protocol":609,"nodes":["123.24.126.128","117.61.73.198"]}]

I’m trying to use KM9, to fetch the list of IP addresses related to a particular Coin version 1.0.4, and for the life of me haven’t been able to figure this out. Would you be able to help?

Here is the start of a solution:

Keyboard Maestro Actions.kmactions (2.1 KB)

Thanks Peter! This worked perfectly for me. Seeing how all this works just reminds me (again) that I'm only scratching the surface of what Keyboard Maestro can do. Amazing!

Thanks for sharing, Peter.
Learning to use JSON in KM is still a challenge for me.

One question: Why did you choose to output the results using the KM Log Action?
The KM Engine Log is not easy to work with.
Also, why export as Actions? Most of are use to importing Macro, which of course create a new Macro in the author's Macro Group.
Double-clicking on an Actions export immediately without warning import in to the currently selected Macro. This has caught me off guard a number of times.

I'd suggest that you change the Actions import to prompt the user to confirm import, and to select the macro where the import is to be made.

Instead, I just substituted a Set Variable with "Append" option.
Then a simple KM Display Window let us easily view the results.

MACRO:   How to Parse JSON Data @Peter

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/0/b/0bd8ca3230bc13cbe9ebbd3ffcb4a7c0dc3990b0.kmmacros">How to Parse JSON Data @Peter.kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### Example Output

![image|690x229](upload://vXn2aB9WpdsXEooGPVi9JamKexL.png) 


---


<img src="/uploads/default/original/3X/4/9/4995f1151d7f6e71edfdc3fbe3e277faaa4c8f3b.png" width="704" height="1768">

I just have a Terminal window with

tail -f ~/Library/Logs/Keyboard\ Maestro/Engine.log

so the log is easily visible, doesn't require any user interaction, and can log multiple things easily as I progress.

That is a self fulfilling statement - if no one ever posts actions then people will never be used to importing actions. I wasn't creating a macro, I don't care how it is triggered or where the data comes from, so in this case a sequence of actions is all that is necessary to show how it is done.

Hi Peter,

Zooming out a little, I had this task to solve where I knew that I receive JSON as input, I knew that I needed to extract some data from that JSON, and I knew that KM9 has native support for JSON.

After some googling, I landed on the latest KM documentation, but I wasn't able to even begin to understand from the documentation how to solve my problem. (Granted, I'm not a tech expert by any means, so when the documentation says something like "JSON is like Xpath", that might short-cut usage understanding for some people, but not me.)

With that as context, I have two questions:

  1. Do you believe I somehow overlooked (or didn't find) the part of the documentation that would have allowed me to solve this problem without having to interrupt you with an email (or post the question to the forum?)

  2. You remember those old O'Reilly "Cookbook" books? I often found those far more useful than documentation. Use example often IMO communicate understanding far better than documentation, too. Perhaps it would be too big a task, since the scope of KM is so large, but it seems that "you get some JSON from a source, and need to grab some data from it" would be such a common use case that a usage example documented somewhere would be tremendously useful. (I guess one is documented here now!)

Anyway, not meant as criticism by any means, just feedback.

1 Like