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.
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":"(.*?)"
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.
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?
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.
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.