How do I use %JSONValue%

I need some help with using %JSONValue%. Assuming this is the best way to get a value out of a json table? I'm calling an llm model though curl and getting the following back which I then place in a "Set Variables to JSON" variable (ModelReturn)"

{"id":"chatcmpl-d82540ff-c613-4f76-b81e-46c51271f280","object":"chat.completion","created":1742043176,"model":"llama-3.3-70b-versatile","choices":[{"index":0,"message":{"role":"assistant","content":"This is a test."},"logprobs":null,"finish_reason":"stop"}],"usage":{"queue_time":0.135583931,"prompt_tokens":59,"prompt_time":0.009509498,"completion_tokens":6,"completion_time":0.021818182,"total_tokens":65,"total_time":0.03132768},"system_fingerprint":"fp_4e32347616","x_groq":{"id":"req_01jpcxtpypec9szb1fgr1gy25w"}}

I want to get the value of "content" but I'm having no luck with this. I have tried with tons of variations but nothing seems to work.

%JSONValue%ModelReturn.choices.[0].message.content%

I hate asking for help because I feel I should be able to figure this out but I'm out of ideas. Maybe JSONValue is the wrong approach?

I expect someone will be able to come up with a more JSONy answer, but you could search the string to extract the text in quotes after the word "content" with this regular expression:
.*content":"(.*?)"

... or you could ask the LLM? :wink:

You have an unnecessary period after choices.

This returns :"This is a test."
%JSONValue%ModelReturn.choices[1].message.content%

1 Like

And, as @CRLF's edit shows, %JSONValue% paths are not zero-indexed.

( Index[0] is used to hold the array length,
and the first item in the array is at index arrayName[1] )

See: manual:JSON Paths [Keyboard Maestro Wiki]

2 Likes

Yeah, the last time I did this, I tackled it by building a regular expression, which worked but it took me a long time. I was hoping this time to figure out how to do it with JSON calls, which should make it a lot easier the next time I need to do this sort of thing or extract other values.

2 Likes

Oh, that was it. I just got it working.

%JSONValue%CallReturn.choices[1].message.content%

The truth is I don't even need to use "Set Variables to JSON".

I can use this indexing directly to the text that the Curl call gave me back. At least I know it works, which leaves me wondering what is the "Set variable set to Jason" actually do in this case?

1 Like

This is a pretty cool feature that lets you set a bunch of KM variables to the key/values of a JSON dictionary. The names of the variables will be the names of the keys prefixed with whatever you enter in the prefix field.
Set Variables to JSON2

https://wiki.keyboardmaestro.com/action/Set_Variables_to_JSON

Set Variables to JSON-DEMO.kmmacros (2.6 KB)

Here is one way of getting to the wiki:

2 Likes

Yeah, that is cool. Although I'm not sure why I'd want to use variable names like that instead of just addressing them directly with a %JSONValue% string. But I do agree it's cool.

You may not have spotted it in the above example, but KM is creating variables named with your prefix followed by the key of each key/value pair in the JSON dictionary. That makes it a lot easier when you want to get multiple values from a dictionary, and a lot quicker than multiple "Set Variable to..." actions pulling individual %JSONValue%s.

Demo:

JSON demo.kmmacros (3.1 KB)

But yes, unnecessary overhead if you only want to get one value from the dictionary.

2 Likes