Hey All,
An internal user came to me asking for a good way to extract values out of JSON text. They wanted to be able to use dot-notation to extract any key from a json from within an app.
Out of curiosity I asked my new best friend and it helped me out…
The last step was to connect the wires to Tulip, and I came to this:
getJsonValue(getValue("Json Object"),getValue("Json extractor"));
});
getValue('Json extractor', (internalVariable) => {
getJsonValue(getValue("Json Object"),getValue("Json extractor"));
});
function getJsonValue(string, extractor) {
const jsonObject = JSON.parse(string)
if (jsonObject ==null || extractor == null){
return null
}
let keys = extractor.split('.');
let value = jsonObject;
for (let i = 0; i < keys.length; i++) {
if (value.hasOwnProperty(keys[i])) {
value = value[keys[i]];
} else {
return null;
}
}
console.log(value)
setValue("Output",value.toString());
return value;
}
Works like a charm! Starting to wonder where else within Tulip we could leverage ChatGPT-
Pete
customWidget-Json Parse To Text.json (2.7 KB)