Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KRL: Parsing string as JSON

Tags:

json

krl

After using http:get(), I receive back a string from picking the "content" from the hash:

response = http:get(webservice_url, {"key1": value1, "key2": value2});
json_resp = response.pick("$..content");

However, since json_resp is a string and not an actual JSON object, I can't run a command like this:

value = json_resp.pick("$..string");

Is there a way to tell KRL that I want to parse json_resp as JSON? An eval() or something, perhaps?

like image 918
Steve Nay Avatar asked Aug 08 '26 20:08

Steve Nay


1 Answers

The decode() operator does just what you want. It operates on a JSON string, attempting to convert it to a native KRL object. Note that KRL also has encode() which operates on a native KRL object and returns a JSON string representation of that object.

response = http:get(webservice_url, {"key1": value1, "key2": value2});
json_resp = response.pick("$..content").decode();
value = json_resp.pick("$..string");
// will work since json_resp is now a native KRL object
like image 61
Alex Avatar answered Aug 11 '26 14:08

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!