Skip to main content

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?

Link to test document: https://lucid.app/lucidchart/f254848b-d4f3-4b9a-adf3-a7d92162f589/edit?viewport_loc=-11%2C-11%2C1879%2C1048%2CUIi2EcGhkY7N&invitationId=inv_054ad497-3d0c-4739-9a64-20d6f79ae62b

Hi!

 

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

  1. 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.
  2. Add a new custom shape data to this item with shapeData.set (https://developer.lucid.co/extension-api/#data-on-documents). The value I set for this custom shape data was an formula which contained the line for creating an object (https://developer.lucid.co/formulas/#object).

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})`)

});

menu.addMenuItem({
label: 'Test thing 2',
action: 'test',
menuType: MenuType.Main,
});


Feel free to reach out if you have any more questions or if there's anything else we can do to support your Lucid adventures.


Happy diagramming! 🚀✨


Reply