# How to return objects from plugins (SSA, CSA, Element Update & Element Workflow Actions)

**URL:** <https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646>\
**Category:** Plugin builders\
**Created:** [August 21, 2021, 3:55pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646 "2021-08-21T15:55:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jared.gibb](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jared.gibb/32/123329_2.png) [@jared.gibb](https://forum.bubble.io/u/jared.gibb)\
**Post date:** [August 21, 2021, 3:55pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/1 "2021-08-21T15:55:16Z")

</div>

This stumps so many of us when we first start with the plugin editor that I wanted to make a couple quick videos to demonstrate returning an object from your plugin.

Video 1:

## Returning an object from a server-side action

[https://vimeo.com/590273642](https://vimeo.com/590273642)  
– those notes I was writing are below.

```auto
https://api.thecatapi.com/v1/images/search

Getting an object back from the server side actions seems impossible at first but it actually really easy. Let’s start by creating a new plugin! 

We will start by heading to the api editor, adding a ‘dummy’ api call, and then defining the object that we want to have returned to us! 

To do this, we’ll need to use a ‘dummy’ url so that bubble knows we mean business (or that they know it’s going to work. They put a safeguard on the api calls so you MUST call a valid API service at least once and it must return something. Otherwise, without calling the dummy API, you cant return the object appropriately. 

SO, let’s go! 

P.s. we’ll be using the URL at the top of this doc for the dummy API call

So notice what i did there. 

I initialize the API, save the call, and then click manually enter the response. 

Here is where we will define the object we want to return. 

And sometimes the api editor is a pain, so let’s validate that JSON

Fun fact, the editor doesnt like malformed jsons or single quotes. So fix that! 

{
"string":"asdfasdf",
    "boolean": false,
    "number": 4,
    "list":["asdf","asdf"]
}

After you click save, the editor will infer the data type each value should be

Make sure those dropdowns have the correct data type. Bubble doesnt like the single quotes. Use the double quotes 

“”

Now, let’s define our server action to return some data based on the model we provided. 

In the SSA you will need a field to define the type of data we we want to return. It must be “App Type”. What this will allow us to do is use the API response we just defined as the model of data to return within the app. 

Actually, we need to do one more thing. Set the returned value. Set the return value to the field type we set in the prvious step. 

If you plan to return a list of that model you created, set the return value to list. 

IMPORTANTTTT. You must append each key name with 

_p_

This lets the bubble engine display and interpret things correctly. 

You must also remember to return the list of objects

Let’s check it out.

But of course, if you’re not returning correctly, it wont work. So,

You’ll need to return your data as an object that has your list of objects. 

Like this

We’ll now test out the returned data. 

Add a button to start the action!

One thing we need is the RG to display the list. We’ll set the element to have the data type that we just defined.
Sometimes the editor won’t update the name appropriately. Perhaps this is a bubble bug to be reported?

```

Video 2:

## Returning an object from a client-side action

{{not yet recorded}}  
— this one is more difficult but definitely doable!

Video 3:

## Returning an object from a plugin element’s update field.

[https://vimeo.com/590292827](https://vimeo.com/590292827)

```auto
Now that you’ve learned to return them from server side actions, let’s float over to the element itself and see how to return objects there. 

Note, we aren’t updating the API call in this video, just doing it again for show. 

So in short to return the object, 

You’ll need to 

Instantiate an api call to any service and define the return data yourself. 

Now that our API call is ready, we will head over to the element editor and work with it there. It’s a very similar process to the SSA. 

Create the element, define an input as App Type

And define an exposed state whose data type is the input you just defined in the last step. 

What this means is when Data Out publishes any data, it will be in the format defined in the ‘datatype’ input field

With actions that happen client side, we can only return objects using instance.publishState. We dont return an object the same way as in SSA, we only set it up the same way

And that should be it. 

Always test though. 

Even if the element wont have a visual element, it must be dragged on the screen..

Oh yeah. Now that we’re element side, the data comes out in an exposed state which can be bound to another element as the data source. An element like a RG.

We didn't set the exposed state to be a list

Be mindful that any change in the plugin editor will require a refresh in the app editor to be observed. 

```

Video 4:

## Returning an object from a plugin element’s workflow actions

— as a note. you don’t have access to instance from workflow actions. _ **BUT YOU kinda DO** _ if you publish instance to window.

you could do something like this

[https://vimeo.com/590298084](https://vimeo.com/590298084)

```auto
So what you do is save instance to the window (a truly global variable that can be shared between elements and plugins. 

Then you call instance.publishstate from your window variable. 

Other than that, theres no difference. 

```

---

<div class="post-metadata">

**Author:** ![jim5](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@jim5](https://forum.bubble.io/u/jim5)\
**Post date:** [August 22, 2021, 5:07pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/2 "2021-08-22T17:07:58Z")

</div>

> [@jared.gibb](#):
>
> ```auto
> {
> "string":"asdfasdf",
> "boolean": false,
> "number": 4,
> "list":["asdf","asdf"]
> }
> 
> ```

Thank you for these videos! I have found the closed (insider) nature of plugins to be frustrating and limiting. Your videos were a big help in solving various API interactions that require JSON parsing, and how to return that data to our App.

---

<div class="post-metadata">

**Author:** ![jared.gibb](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jared.gibb/32/123329_2.png) [@jared.gibb](https://forum.bubble.io/u/jared.gibb)\
**Post date:** [August 22, 2021, 5:13pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/3 "2021-08-22T17:13:52Z")

</div>

I found it difficult too. Now to make the videos a little more friendly with voiceover, let’s cuts, and more clear instructions!

Glad you found it useful!!

We shouldn’t be limited by lack of knowledge. Doing this allows you to expand your reaches quite a bit in my opinion.

---

<div class="post-metadata">

**Author:** ![lindsay\_knowcode](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/lindsay_knowcode/32/92967_2.png) [@lindsay\_knowcode](https://forum.bubble.io/u/lindsay_knowcode)\
**Post date:** [August 28, 2021, 1:27am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/4 "2021-08-28T01:27:18Z")

</div>

I am astounded how you ever managed to figure this out but I am in awe! 🙌 🙌 🙌 🙌

Super useful.

---

<div class="post-metadata">

**Author:** ![jared.gibb](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jared.gibb/32/123329_2.png) [@jared.gibb](https://forum.bubble.io/u/jared.gibb)\
**Post date:** [August 28, 2021, 1:30am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/5 "2021-08-28T01:30:21Z")

</div>

thanks for the support @lindsay_knowcode the real gold nugget is saving the instance from an element to the window variable. then you can literally publish data between elements.

that and using the API as a data type. it’s 💰 💰 💰

---

<div class="post-metadata">

**Author:** ![viacheslav](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/viacheslav/32/96486_2.png) [@viacheslav](https://forum.bubble.io/u/viacheslav)\
**Post date:** [September 30, 2021, 7:11am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/6 "2021-09-30T07:11:38Z")

</div>

Awesome! You are a bubble-hacker for real!

---

<div class="post-metadata">

**Author:** ![lottemint.md](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/lottemint.md/32/26549_2.png) [@lottemint.md](https://forum.bubble.io/u/lottemint.md)\
**Post date:** [September 30, 2021, 9:20am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/7 "2021-09-30T09:20:17Z")

</div>

> [@jared.gibb](#):
>
> ```auto
> IMPORTANTTTT. You must append each key name with 
> 
> _p_
> 
> ```

This one automatically handles that stuff (helpful for large/nested objects):

> **[GitHub - Lottemint/json-to-bubble-object: Convert a JSON object to a Bubble...](https://github.com/Lottemint/json-to-bubble-object)**
>
> Convert a JSON object to a Bubble object for plugin purposes. (Bubble.is) - GitHub - Lottemint/json-to-bubble-object: Convert a JSON object to a Bubble object for plugin purposes. (Bubble.is)

---

<div class="post-metadata">

**Author:** ![jared.gibb](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jared.gibb/32/123329_2.png) [@jared.gibb](https://forum.bubble.io/u/jared.gibb)\
**Post date:** [September 30, 2021, 10:45am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/8 "2021-09-30T10:45:57Z")

</div>

> [@lottemint.md](#):
>
> This one automatically handles that stuff (helpful for large/nested objects):

This is only relevant for SSA right? Can we use this client side? Looks like it requires a node module to be imported

---

<div class="post-metadata">

**Author:** ![lottemint.md](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/lottemint.md/32/26549_2.png) [@lottemint.md](https://forum.bubble.io/u/lottemint.md)\
**Post date:** [September 30, 2021, 10:50am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/9 "2021-09-30T10:50:28Z")

</div>

> [@jared.gibb](#):
>
> Can we use this client side?

Yup.

```
/**
 * @param {Object} obj
 * @param {string} [param_prefix] - _p_ (default) or _api_c2_.
 * @return {Object}
 */
let convertObject = (obj, param_prefix) => {

    if (typeof obj !== 'object' || (obj && Array.isArray(obj))) return {};

    if (typeof param_prefix !== 'string' || !param_prefix || (typeof param_prefix === 'string' && !['_p_', '_api_c2_'].includes(param_prefix))) param_prefix = '_p_';

    /**
     * @param {Object} obj
     * @param {string} [key_parent]
     * @param {boolean} [is_array]
     * @return {Object}
     */
    const convert = (obj, key_parent, is_array) => {

        let result = {};

        Object.keys(obj).forEach(key => {

            let cell = obj[key], key_new = `${param_prefix}${key}`;

            if (key_parent && !is_array) key_new = `${key_parent}.${key}`;

            if ((!cell && cell !== 0 && cell !== false) || typeof cell === 'undefined') {

                result[key_new] = null

            } else if (typeof cell !== 'object' && !Array.isArray(cell)) {

                result[key_new] = cell

            } else if (typeof cell === 'object' && !Array.isArray(cell)) {

                result = Object.assign(result, convert(cell, key_new))

            } else if (Array.isArray(cell)) {

                if (typeof cell[0] === 'object') {

                    result[key_new] = [];

                    cell.forEach(value => {

                        result[key_new].push(convert(value, key_new, true))
                    })

                } else {

                    // It's not an object array, so treat
                    // it as an array of Bubble primitives.
                    //
                    result[key_new] = cell;
                }
            }
        });

        return result
    };

    return convert(obj)
};

```

---

<div class="post-metadata">

**Author:** ![jared.gibb](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jared.gibb/32/123329_2.png) [@jared.gibb](https://forum.bubble.io/u/jared.gibb)\
**Post date:** [September 30, 2021, 11:10am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/10 "2021-09-30T11:10:26Z")

</div>

Awesome! I hadn’t thought too far ahead! I may find use for this!!

Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![ankur1](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@ankur1](https://forum.bubble.io/u/ankur1)\
**Post date:** [December 10, 2021, 10:08am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/11 "2021-12-10T10:08:08Z")

</div>

Hi @jared.gibb , Thanks for recording videos and putting out here. It help me a lot.

I am trying to return custom objects from plugin action and somewhat able to do so. But go stuck with the last segment. Since bubble plugin need “_p_” prefix with each key but that is not possible (in my case- as I am getting data from third party API), how to map “_p_” prefix key with the normal key (without _p_ prefix).

Hope you are able to understand what I am trying to say.

For Instance-

![Screenshot 2021-12-10 at 3.36.38 PM](https://us1.discourse-cdn.com/bubble/original/3X/7/4/744a88e26ff98a2d3f405267f587678a765e5b43.png)

This type of data required by bubble but I am getting without “_p_” prefix data. Any help you can do it?

Thanks a lot in advance.

---

<div class="post-metadata">

**Author:** ![jared.gibb](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jared.gibb/32/123329_2.png) [@jared.gibb](https://forum.bubble.io/u/jared.gibb)\
**Post date:** [December 10, 2021, 12:06pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/12 "2021-12-10T12:06:16Z")

</div>

Check this out

```auto
const arrayofObjects = [
      {
            name: 'John',
            age: 30

      },
      {
            name: 'Peter',
            age: 25
      },
      {
            name: 'Amy',
            age: 40
      },
];

// 1. write a function that appends _p_ to the beginning of each key in the object
let newArray = arrayofObjects.map(function(item) {
      let newObj = {};
      for (let key in item) {
            newObj['_p_' + key] = item[key];
      }
      return newObj;
}
);
console.log(newArray);

```

---

<div class="post-metadata">

**Author:** ![ankur1](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@ankur1](https://forum.bubble.io/u/ankur1)\
**Post date:** [December 10, 2021, 1:37pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/13 "2021-12-10T13:37:22Z")

</div>

> [@jared.gibb](#):
>
> ```auto
> let newArray = arrayofObjects.map(function(item) {
> let newObj = {};
> for (let key in item) {
> newObj['_p_' + key] = item[key];
> }
> return newObj;
> }
> );
> 
> ```

Thanks it works like a charm.

---

<div class="post-metadata">

**Author:** ![agoo7714](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/agoo7714/32/129385_2.png) [@agoo7714](https://forum.bubble.io/u/agoo7714)\
**Post date:** [January 24, 2022, 9:50am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/15 "2022-01-24T09:50:31Z")

</div>

@lottemint.md

Sorry this might be a very newbie question but how would I import this module into my plugin? I know how to do it using cdnjs as source links in html but I’m not sure how to do it from github as I can never find these links.

---

<div class="post-metadata">

**Author:** ![exception-rambler](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/exception-rambler/32/152704_2.png) [@exception-rambler](https://forum.bubble.io/u/exception-rambler)\
**Post date:** [January 24, 2022, 10:36am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/16 "2022-01-24T10:36:46Z")

</div>

@lottemint.md has hosted this on NPM and the module can be drawn directly down into your server side function. It’s called `json-to-bubble-object` and you add it simply by declaring some variable using `require('json-to-bubble-object')` - see the example below.

It will then automatically be added to the Node `package.json` though you’ll need to click on the red warning message to perform an initial sync of the module.

 ![Screenshot 2022-01-24 at 10.31.46](https://us1.discourse-cdn.com/bubble/original/3X/6/3/63c3ed53664f64cc8090f1a8946b70a1e19bc3e8.jpeg)

---

<div class="post-metadata">

**Author:** ![lottemint.md](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/lottemint.md/32/26549_2.png) [@lottemint.md](https://forum.bubble.io/u/lottemint.md)\
**Post date:** [January 24, 2022, 12:15pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/17 "2022-01-24T12:15:36Z")

</div>

> [@exception-rambler](#):
>
> NPM and the module can be drawn directly down into your server side function

Yup.  
It also can be run client-side if you extract the code from this module:

> [@How to return objects from plugins (SSA, CSA, Element Update & Element Workflow Actions)](http://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/9):
>
> Yup. /\*\* \* @param {Object} obj \* @param {string} [param\_prefix] - \_p\_ (default) or \_api\_c2\_. \* @return {Object} \*/ let convertObject = (obj, param\_prefix) =\> { if (typeof obj !== 'object' || (obj && Array.isArray(obj))) return {}; if (typeof param\_prefix !== 'string' || !param\_prefix || (typeof param\_prefix === 'string' && !['\_p\_', '\_api\_c2\_'].includes(param\_prefix))) param\_prefix = '\_p\_'; /\*\* \* @param {Object} obj \* @param {string} [key\_parent] \* @param {boo…

---

<div class="post-metadata">

**Author:** ![agoo7714](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/agoo7714/32/129385_2.png) [@agoo7714](https://forum.bubble.io/u/agoo7714)\
**Post date:** [January 26, 2022, 10:09pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/18 "2022-01-26T22:09:50Z")

</div>

Thanks @lottemint.md @exception-rambler I’ve got the module working. I’m now having trouble returning the data. what function would I use to return the data. The response I’m getting from the module is

```
{"_p_mins":5,"_p_price":"36389.29688301"}

```

This method I have here works

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/4/5/45a583cc0a180a84fb9dcffbc744755a02a9e3f3.png)

The api\_response field is just a test field that returns the api response. I have tried the following

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/a/c/acd5e83306553806125c620b4d0bbddd221a0aff.png)

but that isn’t working. There must be a simple function that I’m missing.

For some reason when I try these methods, I get null responses, which I know is me doing something wrong because when I display it under the “api\_response” it returns correctly

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/d/9/d9522421c6299d2467af391c24dfa67237aeb638.png)

---

<div class="post-metadata">

**Author:** ![agoo7714](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/agoo7714/32/129385_2.png) [@agoo7714](https://forum.bubble.io/u/agoo7714)\
**Post date:** [January 27, 2022, 5:23am UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/19 "2022-01-27T05:23:07Z")

</div>

This has been solved! Thanks got it working

---

<div class="post-metadata">

**Author:** ![jared.gibb](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jared.gibb/32/123329_2.png) [@jared.gibb](https://forum.bubble.io/u/jared.gibb)\
**Post date:** [January 27, 2022, 12:15pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/20 "2022-01-27T12:15:32Z")

</div>

Wanna share your solution or what was keeping you from passing information along?

---

<div class="post-metadata">

**Author:** ![agoo7714](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/agoo7714/32/129385_2.png) [@agoo7714](https://forum.bubble.io/u/agoo7714)\
**Post date:** [January 27, 2022, 7:47pm UTC](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646/21 "2022-01-27T19:47:31Z")

</div>

ofcourse, I need to get into the habit of doing this. My solution was the following code

```auto
var body = JSON.parse(response.body);

const {convert} = require('json-to-bubble-object');

var bodystr = function(body){
  Object.keys(body).forEach(function(key){ 
      if (typeof body[key] == "number"){
      body[key] = JSON.stringify(body[key]) }
  		else 
  		{body[key] = body[key] }});
  return body;
}
        
let returnlist = []
returnlist.push(convert(bodystr(body)))
                
    return {
        "result": returnlist
    
    }
}

```

I think I got to the answer by trying out a bunch of different combinations but key things I had to do was make sure all elements of the object literal were strings (as numbers threw an error), and the convert function for the json-to-bubble-object was setup properly.

EDIT: see the following post for better script, I was getting errors with returning numbers in this script because when I initialised the APIs in the API connector, I didn’t specify number to some variables which were numbers.

[Next page](https://forum.bubble.io/t/how-to-return-objects-from-plugins-ssa-csa-element-update-element-workflow-actions/165646.md?page=2)
