I am trying to develop functionality for a custom Extension and I would like to reference a value from a JSON object stored in a Field. The documentation has formulas (such as GET(obj, val)) that I am trying to use. However, they are not working and I am running into issues.
I then tried to figure out the intended format of objects by using the examples provided in the documentation. However, these do not work and I am needing help.
Using this page: https://developer.lucid.co/formulas/#get, I am trying to get the GET formula of the Object class working. There is a Field called “Object” with values “{ "A": 1, "B": 2, "C": 3, "D": 4, "E": 5, }”. Then, values can be retrieved from that object using =GET(@Object, “A”).
I tried to simulate this on my own document but ran into issues. Can someone help me understand the format objects can be stored in? Specifically, can I store an object in JSON format in a field and use the GET formula to get value from it?
The issue is that Lucid doesn’t create the data as an object simply by putting it in as
{"A": 1, "B": 2}
etc.
Instead, try this - you can create your data as an object using the OBJECT formula. Once the data is actually in an object, GET will function as expected.
Hopefully this is helpful :)
Thanks. Yes, I have seen how to use the function to create the object using key value pair parameters.
However, my main question revolves around how (or if) JSON data could be converted to a Lucid Object, so that property values could be retrieved from it using the GET function.
Do you have any recommendations for how to manipulate the JSON data into a format that could be passed to the Object function? I have not see any easy or intuitive ways to achieve this using other functions within Lucid
Or better yet, could the Object function be updated to accept JSON data as a parameter instead of only key value pairs?
Hi Nick!
Great to see you exploring the possibilities of Lucid Extension!
At the crux, you want to add a custom property to a shape and the value of this custom property is a OBJECT whose values are parsed from a JSON objects.
The steps I followed in the gif are
Get an itemId of a shape. I did this by using getSelectedItems which returns the items that are selected and then I extracted out the ids from that item.
I used a small snippet to convert a JSON object into a string format that the OBJECT inside the formula expects.
Here’s the code I used in the gif. This should be in the extension.ts inside an editorextension.
import {EditorClient, Menu, MenuType, Viewport,ElementProxy} from 'lucid-extension-sdk';
const client = new EditorClient(); const menu = new Menu(client); const viewport = new Viewport(client);
client.registerAction('test', () => {
//Parse Json Data let fakseJsonData = '{"name": "John", "age": 30, "city": "New York"}'; let parsedData = JSON.parse(fakseJsonData) as {"name": string, "age": number, "city": string};
//convert json object to a string format to be used in a formula let result = "" for (const key, value] of Object.entries(parsedData)) { result += `"${key}",` result += `"${value}"` }
//Get shape from selection const itemIdsToDup = viewport.getSelectedItems().map(item => item.id); let ep = new ElementProxy(itemIdsToDupi0],client)
//Set the customField ep.shapeData.set('customField',`=OBJECT(${result})`)