Extracting useful info from JS object

Wondering if anyone can help me. I’m trying to find a way (with the action button script) to extract useful info from the objects returned when executing commands within an action button.

So for example, I use the following

const fibery = context.getService(‘fibery’);
const e = await fibery.executeSingleCommand({
“command”: “fibery.entity/query”,
“args”: {
“query”: {
“q/from”: “App/Type”,
“q/select”: [
“fibery/id”,
],
“q/limit”: 1
}
}
});

and I would like to be able to get the id as a string.

I thought that maybe I could just use something like

console.log(e.fibery/id)

but no luck.

So I’m wondering if any software gurus out there have any suggestions. In fact, a general guide for how to parse any object that is returned when a command is executed.

I have no formal software training, and have not worked with JS before, so please forgive my ignorance.

When accessing child nodes that are not a-Z or the name of the node is a variable, you need to enclose it in brackets. This also applies to any number of childnodes, like a[b][c].d.[e].f.g

var name = "fibery/id";
console.log(e[name]);

or

console.log(e["fibery/id"])

Furthermore it returns an array even though you limit result to 1, as indicated by surrounding brackets [].

You would first need to access the index of the item you want to use, so 0 for first, 1 for second, etc.
To get the ID based on your example, it would be:

console.log(e[0]["fibery/id"])

I would also like to add an extra note that the method you use returns an object, not JSON. Knowing this distinction may come handy at some point. An object in JavaScript is a bit more free to contain other things, like functions, maps, sets, etc.

JSON on the other hand can only contain Boolean, Number, String, Array, and “Objects” containing any of the former. JSON is actually plain text, just highly structured. You usually get a JSON string from API’s, then any library you use may or may not convert it to an Object automatically or through a setting.

The method you use likely gets returned JSON, but automatically convert it to a JavaScript Object.

It’s relatively easy to convert between the two:
const myObject = JSON.parse(jsonString);
const jsonString = JSON.stringify(myObject);.

3 Likes

By the way you can customize message, that is shown in tooltip on the top of the page after script execution.
Just add return statement to your script with value you want.
This might be more convenient in your case, then writing to console.

return e[0]["fibery/id"]

(As @Haslien suggested above)

3 Likes

Thanks @Haslien and @Sergey_Truhtanov :+1:
Just what I needed.

I had reached the conclusion from a bit of googling that the forward slash could be part of the problem, and I did learn about the parse and stringify functions along the way.

I didn’t know about the use of [ ]
Plus, although I recognised that in many of the cases I would be returning an array, I didn’t realise that I couldn’t send an array to the console log (even though it seems to be possible to send an object).

As I said, I have no formal education in sw so most of what I have done has been through trial&error + google + guesswork. I am v grateful that you responded so quickly with the right answer :clap:

[edit: I changed the title of the post to be slightly more correct (I hope!)]

That’s useful news. It also reminded me to try custom error message by using throw new Error("my message") and it worked :slight_smile:

Time to update some of our buttons :thinking:

1 Like

Can you send multiple sequential messages to the tooltip (i.e. without returning)?

I think this is a good resource if you want to learn Javascript: https://eloquentjavascript.net/. You get to type everything into the browser too, so you don’t yet have to conflate learning the language with learning deployment / setup etc at the same time.

1 Like

@Chr1sG
Nope. Just store temporary results in a variable and then concat them to result string somehow.

1 Like