Using calulation field + javascript to parse JSON
I was wondering if anyone has used a calculation field to parse JSON saved in another field in PODIO
For example, I have a JSON string saved in a field (called JSONResponse). I am trying to get to the value of a field (called id) in that JSON string.
I've tried this but I keep getting errors:
var JSONstring = @JSONResponse
var obj = JSON.parse(JSONstring)
obj.id;
-
Hi Ryan,
The problem here is that when you save your template the calculation field is validated against a sample value that Podio injects - specifically the string "Script field example text"
Since "Script field example text" is not valid JSON, it results in a JavaScript error when attempted parsed.
The solution to the problem is a little hacky, but it will work:
Simply wrap your "JSON.parse()" invocation in a try/catch clause like so:try { String(JSON.parse(@JSONResponse).id); } catch (e) { "Could not parse JSON" }
Let me hear if this solves your problem or not.
Regards,
Marc, Podio
-
I've tried but failed. See details here: https://help.podio.com/hc/communities/public/questions/202223098-How-to-parse-JSON-in-a-calculation-field-?locale=en-us
Please sign in to leave a comment.
Comments
7 comments