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

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

Super useful.

1 Like

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 :moneybag: :moneybag: :moneybag:

3 Likes

Awesome! You are a bubble-hacker for real!

1 Like

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

1 Like

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

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)
};
5 Likes

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

Thanks for sharing!

1 Like

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

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.

Check this out

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);
1 Like

Thanks it works like a charm.

2 Likes

@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.

@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.

3 Likes

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

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

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

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

This has been solved! Thanks got it working

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

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

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.

Hi @lottemint.md I have been using your module and it’s working great. Only issue I’ve found Is I am having trouble getting a response when using arrays of objects.

I am using this simple code to return the data.

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

const {convert} = require('json-to-bubble-object');
    
        
let returnlist = []
returnlist.push(convert(body))
                
    return {
        "result": returnlist,
    
    }
}

(the response.body is simply the JSON response from the API). For the following response which is a singular objects such as


{"symbol":"BTCUSDT","priceChange":"181.33000000","priceChangePercent":"0.499","weightedAvgPrice":"36644.71400220","prevClosePrice":"36366.40000000","lastPrice":"36547.73000000","lastQty":"0.08275600","bidPrice":"36546.00000000","bidQty":"0.07223800","askPrice":"36551.16000000","askQty":"0.05581300","openPrice":"36366.40000000","highPrice":"37489.80000000","lowPrice":"35533.92000000","volume":"3393.15320200","quoteVolume":"124341128.65294207","openTime":1643281515271,"closeTime":1643367915271,"firstId":2418729,"lastId":2506758,"count":88030}

This code is returning perfectly and I am getting responses as expected. When I run the same code on an array of objects such as the following, I get a null response. Do you know why this is?

[{"symbol":"BNBBUSD","priceChange":"-170.00000000","priceChangePercent":"-45.946","weightedAvgPrice":"420.48349812","prevClosePrice":"600.00000000","lastPrice":"200.00000000","lastQty":"2.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"350.00000000","askQty":"50.00000000","openPrice":"370.00000000","highPrice":"532.00000000","lowPrice":"200.00000000","volume":"71.81000000","quoteVolume":"30194.92000000","openTime":1643276491573,"closeTime":1643362891573,"firstId":1733,"lastId":1769,"count":37},{"symbol":"BTCBUSD","priceChange":"482142.00000000","priceChangePercent":"93.103","weightedAvgPrice":"613856.95824122","prevClosePrice":"1000000.00000000","lastPrice":"999999.00000000","lastQty":"0.00010000","bidPrice":"250000.00000000","bidQty":"0.00009000","askPrice":"999999.00000000","askQty":"0.00552000","openPrice":"517857.00000000","highPrice":"1000000.00000000","lowPrice":"250000.00000000","volume":"0.03700300","quoteVolume":"22714.54902580","openTime":1643281294370,"closeTime":1643367694370,"firstId":2153,"lastId":2340,"count":188},{"symbol":"ETHBUSD","priceChange":"-45973.11000000","priceChangePercent":"-94.884","weightedAvgPrice":"2984.76894256","prevClosePrice":"25039.43000000","lastPrice":"2479.00000000","lastQty":"0.21779000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"2479.00000000","askQty":"0.15333000","openPrice":"48452.11000000","highPrice":"48452.11000000","lowPrice":"1982.76000000","volume":"4.38794000","quoteVolume":"13096.98703380","openTime":1643278093979,"closeTime":1643364493979,"firstId":7456,"lastId":7550,"count":95},{"symbol":"LTCBUSD","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"61.00000000","prevClosePrice":"60.00000000","lastPrice":"61.00000000","lastQty":"1.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"61.00000000","highPrice":"61.00000000","lowPrice":"61.00000000","volume":"1.00000000","quoteVolume":"61.00000000","openTime":1642790849411,"closeTime":1642877249411,"firstId":279,"lastId":279,"count":1},{"symbol":"TRXBUSD","priceChange":"0.03232000","priceChangePercent":"32.000","weightedAvgPrice":"0.11076213","prevClosePrice":"0.10100000","lastPrice":"0.13332000","lastQty":"1523.50000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.13332000","askQty":"88476.50000000","openPrice":"0.10100000","highPrice":"0.13332000","lowPrice":"0.10100000","volume":"16818.90000000","quoteVolume":"1862.89722000","openTime":1643122833061,"closeTime":1643209233061,"firstId":1249,"lastId":1294,"count":46},{"symbol":"XRPBUSD","priceChange":"-0.01810000","priceChangePercent":"-2.854","weightedAvgPrice":"0.61992932","prevClosePrice":"0.63450000","lastPrice":"0.61610000","lastQty":"1000.50000000","bidPrice":"0.61610000","bidQty":"379.20000000","askPrice":"0.61760000","askQty":"1123.30000000","openPrice":"0.63420000","highPrice":"0.63420000","lowPrice":"0.61020000","volume":"108098.80000000","quoteVolume":"67013.61575000","openTime":1643275083974,"closeTime":1643361483974,"firstId":8697,"lastId":8878,"count":182},{"symbol":"BNBUSDT","priceChange":"15.70000000","priceChangePercent":"4.263","weightedAvgPrice":"378.25471882","prevClosePrice":"368.30000000","lastPrice":"384.00000000","lastQty":"11.24000000","bidPrice":"384.00000000","bidQty":"1.07000000","askPrice":"384.10000000","askQty":"6.70000000","openPrice":"368.30000000","highPrice":"1850.00000000","lowPrice":"74.20000000","volume":"71349.31000000","quoteVolume":"26988213.19210000","openTime":1643281621223,"closeTime":1643368021223,"firstId":343601,"lastId":355850,"count":12250},{"symbol":"BTCUSDT","priceChange":"108.79000000","priceChangePercent":"0.299","weightedAvgPrice":"36644.68429030","prevClosePrice":"36409.39000000","lastPrice":"36518.19000000","lastQty":"0.06572100","bidPrice":"36522.25000000","bidQty":"0.02846000","askPrice":"36524.95000000","askQty":"0.06160200","openPrice":"36409.40000000","highPrice":"37489.80000000","lowPrice":"35533.92000000","volume":"3395.86321500","quoteVolume":"124440335.40672936","openTime":1643281621290,"closeTime":1643368021290,"firstId":2418840,"lastId":2506928,"count":88089},{"symbol":"ETHUSDT","priceChange":"-662.84000000","priceChangePercent":"-30.661","weightedAvgPrice":"1820.66870669","prevClosePrice":"2161.82000000","lastPrice":"1498.98000000","lastQty":"0.06000000","bidPrice":"1476.50000000","bidQty":"0.54404000","askPrice":"1521.46000000","askQty":"2.55087000","openPrice":"2161.82000000","highPrice":"2161.82000000","lowPrice":"1241.43000000","volume":"120.85860000","quoteVolume":"220043.47095490","openTime":1643281618572,"closeTime":1643368018572,"firstId":53006,"lastId":53645,"count":640},{"symbol":"LTCUSDT","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"438.17565832","prevClosePrice":"461.06000000","lastPrice":"461.06000000","lastQty":"2.48345000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"461.06000000","askQty":"101.80756000","openPrice":"461.06000000","highPrice":"461.06000000","lowPrice":"350.00000000","volume":"10.28616000","quoteVolume":"4507.14492960","openTime":1643265940862,"closeTime":1643352340862,"firstId":2312,"lastId":2331,"count":20},{"symbol":"TRXUSDT","priceChange":"0.00266000","priceChangePercent":"4.890","weightedAvgPrice":"0.06827562","prevClosePrice":"0.05440000","lastPrice":"0.05706000","lastQty":"1752.50000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.05706000","askQty":"356.50000000","openPrice":"0.05440000","highPrice":"0.10000000","lowPrice":"0.05440000","volume":"42086.70000000","quoteVolume":"2873.49573600","openTime":1643260927793,"closeTime":1643347327793,"firstId":2646,"lastId":2709,"count":64},{"symbol":"XRPUSDT","priceChange":"7.13270000","priceChangePercent":"1.858","weightedAvgPrice":"386.25031010","prevClosePrice":"391.04500000","lastPrice":"391.04500000","lastQty":"162.80000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"391.04500000","askQty":"49663.30000000","openPrice":"383.91230000","highPrice":"391.04500000","lowPrice":"300.00000000","volume":"184.10000000","quoteVolume":"71108.68209000","openTime":1643258286905,"closeTime":1643344686905,"firstId":9343,"lastId":9361,"count":19},{"symbol":"BNBBTC","priceChange":"0.00016900","priceChangePercent":"1.660","weightedAvgPrice":"0.01019432","prevClosePrice":"0.01300000","lastPrice":"0.01035000","lastQty":"0.95000000","bidPrice":"0.01149200","bidQty":"0.20000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.01018100","highPrice":"0.01039000","lowPrice":"0.01018100","volume":"13.03000000","quoteVolume":"0.13283204","openTime":1643117676485,"closeTime":1643204076485,"firstId":316,"lastId":324,"count":9},{"symbol":"ETHBTC","priceChange":"-0.00374300","priceChangePercent":"-5.464","weightedAvgPrice":"0.06492456","prevClosePrice":"0.08000000","lastPrice":"0.06475700","lastQty":"64.01412000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.07780000","askQty":"70.92430000","openPrice":"0.06850000","highPrice":"0.06850000","lowPrice":"0.06475700","volume":"67.01412000","quoteVolume":"4.35086236","openTime":1643276240832,"closeTime":1643362640832,"firstId":3103,"lastId":3104,"count":2},{"symbol":"LTCBTC","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00000000","prevClosePrice":"0.00319700","lastPrice":"0.00000000","lastQty":"0.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.00000000","highPrice":"0.00000000","lowPrice":"0.00000000","volume":"0.00000000","quoteVolume":"0.00000000","openTime":1642974551206,"closeTime":1643060951206,"firstId":-1,"lastId":-1,"count":0},{"symbol":"TRXBTC","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00000222","prevClosePrice":"0.00000165","lastPrice":"0.00000222","lastQty":"6060.00000000","bidPrice":"0.00000168","bidQty":"59.60000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.00000222","highPrice":"0.00000222","lowPrice":"0.00000222","volume":"6060.00000000","quoteVolume":"0.01345320","openTime":1643121796546,"closeTime":1643208196546,"firstId":5,"lastId":5,"count":1},{"symbol":"XRPBTC","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00001650","prevClosePrice":"0.00001650","lastPrice":"0.00001650","lastQty":"11.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00001650","askQty":"12489.00000000","openPrice":"0.00001650","highPrice":"0.00001650","lowPrice":"0.00001650","volume":"11.00000000","quoteVolume":"0.00018150","openTime":1643194419610,"closeTime":1643280819610,"firstId":1536,"lastId":1536,"count":1},{"symbol":"LTCBNB","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00000000","prevClosePrice":"0.17450000","lastPrice":"0.00000000","lastQty":"0.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.00000000","highPrice":"0.00000000","lowPrice":"0.00000000","volume":"0.00000000","quoteVolume":"0.00000000","openTime":1642847149592,"closeTime":1642933549592,"firstId":-1,"lastId":-1,"count":0},{"symbol":"TRXBNB","priceChange":"0.00000030","priceChangePercent":"0.200","weightedAvgPrice":"0.00014987","prevClosePrice":"0.00000000","lastPrice":"0.00015000","lastQty":"1000.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.00014970","highPrice":"0.00015010","lowPrice":"0.00014970","volume":"4000.00000000","quoteVolume":"0.59950000","openTime":1641243788364,"closeTime":1641330188364,"firstId":0,"lastId":3,"count":4},{"symbol":"XRPBNB","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00162700","prevClosePrice":"0.00162700","lastPrice":"0.00162700","lastQty":"90000.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.00162700","highPrice":"0.00162700","lowPrice":"0.00162700","volume":"90000.00000000","quoteVolume":"146.43000000","openTime":1642126497120,"closeTime":1642212897120,"firstId":8,"lastId":906,"count":899}]

SORRY about the giant objects, but just wanted to use the real data I am using. The main point is that the first body is a singular objects, and the second is an array of objects.

Thanks

@jared.gibb Would you be able to shed some light on how to return arrays of objects? I’m struggling with this on a variety of API calls similar to this above comment.

Did you see the very first video in this thread? :crazy_face:

Where do you get lost? Wanna share a code example?

1 Like