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

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

Hi @jared.gibb , yes the first video was extremely helpful and I’, successfully returning objects from my plugin calls. My issue is when I have an array of objects rather than a singular object to return.

My action code is the following:

function(properties, context) {
  
    var burl = ''
    , query = ''
    , method = 'GET'
    , testa = '';
    
 
    const ts = Date.now();

var crypto = require('crypto-js')
    

                  , text = 'timestamp=' + Date.now()
                  , skey = properties.secret_key
                  , hash
					, symbolinput = properties.symbol.trim()
					, symbola = ''					
					, symbol = '';
    
    if (properties.testnet.toString() == "false")
    	burl = 'https://api.binance.com/api/v3/';
    else
        burl = 'https://testnet.binance.vision/api/v3/';
   

if (symbolinput.length > 0){
		symbola = symbolinput.toUpperCase();
    	symbol = "?symbol=" + symbola.replace(/\s+/g, ' ').trim()
}
    
if (properties.endpoint.trim() == "Current Average Price"){
    var query = 'avgPrice' + symbol;
}

if (properties.endpoint.trim() == "24hr Ticker Price Change Statistics"){
    var query = 'ticker/24hr' + symbol;
}
    				
var hash = crypto.HmacSHA256(text, skey).toString()
	, url = burl + query

const options = {
    url: url
    ,method: method
    ,headers: {
        "Content-Type": "application/json"
    }
}


const response = context.request(options);
	console.log(response);
    

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

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

When I specify a Symbol, the result is returned as the following


Demo Login
{"symbol":"BTCUSDT","priceChange":"3682.66000000","priceChangePercent":"9.959","weightedAvgPrice":"38562.22336739","prevClosePrice":"36978.77000000","lastPrice":"40661.51000000","lastQty":"0.00491600","bidPrice":"40661.53000000","bidQty":"0.05017100","askPrice":"40661.54000000","askQty":"0.05951600","openPrice":"36978.85000000","highPrice":"60000.00000000","lowPrice":"7500.00000000","volume":"3571.14095500","quoteVolume":"137711135.18314058","openTime":1643929763213,"closeTime":1644016163213,"firstId":332519,"lastId":475709,"count":143191}

and the object is returned and displayed in my RG

But when I don’t specify a Symbol (which returns all symbols data), the response is an ARRAY of objects like as follows, and all the fields are displayed as null.


Demo Login
[{"symbol":"BNBBUSD","priceChange":"-260.30000000","priceChangePercent":"-72.245","weightedAvgPrice":"261.29676153","prevClosePrice":"380.00000000","lastPrice":"100.00000000","lastQty":"0.10000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"400.00000000","askQty":"16.61000000","openPrice":"360.30000000","highPrice":"400.00000000","lowPrice":"100.00000000","volume":"101.90000000","quoteVolume":"26626.14000000","openTime":1643928538140,"closeTime":1644014938140,"firstId":167,"lastId":229,"count":63},{"symbol":"BTCBUSD","priceChange":"-397015.20000000","priceChangePercent":"-96.928","weightedAvgPrice":"199767.05017855","prevClosePrice":"15731.00000000","lastPrice":"12584.80000000","lastQty":"0.01770000","bidPrice":"12584.80000000","bidQty":"0.00794600","askPrice":"800000.00000000","askQty":"0.00312900","openPrice":"409600.00000000","highPrice":"1000000.00000000","lowPrice":"12584.80000000","volume":"0.10249400","quoteVolume":"20474.92404100","openTime":1643929869031,"closeTime":1644016269031,"firstId":984,"lastId":1112,"count":129},{"symbol":"ETHBUSD","priceChange":"317.96000000","priceChangePercent":"12.166","weightedAvgPrice":"2682.22056263","prevClosePrice":"2715.36000000","lastPrice":"2931.38000000","lastQty":"0.03411000","bidPrice":"2612.40000000","bidQty":"0.23518000","askPrice":"2931.38000000","askQty":"0.03483000","openPrice":"2613.42000000","highPrice":"2931.38000000","lowPrice":"2612.40000000","volume":"11.89729000","quoteVolume":"31911.15587760","openTime":1643929208027,"closeTime":1644015608027,"firstId":1201,"lastId":1271,"count":71},{"symbol":"LTCBUSD","priceChange":"-10.40000000","priceChangePercent":"-9.420","weightedAvgPrice":"109.56956955","prevClosePrice":"110.00000000","lastPrice":"100.00000000","lastQty":"0.18116000","bidPrice":"100.00000000","bidQty":"4.81884000","askPrice":"110.40000000","askQty":"7.91238000","openPrice":"110.40000000","highPrice":"110.40000000","lowPrice":"100.00000000","volume":"2.26878000","quoteVolume":"248.58924800","openTime":1643927358794,"closeTime":1644013758794,"firstId":6,"lastId":13,"count":8},{"symbol":"TRXBUSD","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00000000","prevClosePrice":"0.06018000","lastPrice":"0.00000000","lastQty":"0.00000000","bidPrice":"0.05955000","bidQty":"1000.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.00000000","highPrice":"0.00000000","lowPrice":"0.00000000","volume":"0.00000000","quoteVolume":"0.00000000","openTime":1643846868283,"closeTime":1643933268283,"firstId":-1,"lastId":-1,"count":0},{"symbol":"XRPBUSD","priceChange":"-0.01760000","priceChangePercent":"-2.940","weightedAvgPrice":"0.60186777","prevClosePrice":"0.59880000","lastPrice":"0.58100000","lastQty":"172.10000000","bidPrice":"0.57500000","bidQty":"1652.20000000","askPrice":"0.59310000","askQty":"172.10000000","openPrice":"0.59860000","highPrice":"0.61060000","lowPrice":"0.56100000","volume":"108426.80000000","quoteVolume":"65258.59666000","openTime":1643928550344,"closeTime":1644014950344,"firstId":605,"lastId":782,"count":178},{"symbol":"BNBUSDT","priceChange":"26.80000000","priceChangePercent":"7.251","weightedAvgPrice":"380.08239973","prevClosePrice":"369.50000000","lastPrice":"396.40000000","lastQty":"6.35000000","bidPrice":"396.40000000","bidQty":"2.28000000","askPrice":"396.50000000","askQty":"6.92000000","openPrice":"369.60000000","highPrice":"398.20000000","lowPrice":"80.00000000","volume":"41526.98000000","quoteVolume":"15783674.21180000","openTime":1643929863662,"closeTime":1644016263662,"firstId":70606,"lastId":78117,"count":7512},{"symbol":"BTCUSDT","priceChange":"3647.90000000","priceChangePercent":"9.859","weightedAvgPrice":"38564.23034247","prevClosePrice":"37000.16000000","lastPrice":"40647.14000000","lastQty":"0.00250000","bidPrice":"40647.13000000","bidQty":"0.07019500","askPrice":"40647.14000000","askQty":"0.06589400","openPrice":"36999.24000000","highPrice":"60000.00000000","lowPrice":"7500.00000000","volume":"3568.59494800","quoteVolume":"137620117.57363274","openTime":1643929870517,"closeTime":1644016270517,"firstId":332606,"lastId":475743,"count":143138},{"symbol":"ETHUSDT","priceChange":"-79.19000000","priceChangePercent":"-7.999","weightedAvgPrice":"946.20699162","prevClosePrice":"992.02000000","lastPrice":"910.85000000","lastQty":"0.01207000","bidPrice":"909.03000000","bidQty":"0.13365000","askPrice":"912.67000000","askQty":"18.62675000","openPrice":"990.04000000","highPrice":"2373.20000000","lowPrice":"463.14000000","volume":"581.75887000","quoteVolume":"550464.31023250","openTime":1643929804202,"closeTime":1644016204202,"firstId":1614,"lastId":2846,"count":1233},{"symbol":"LTCUSDT","priceChange":"-10.50000000","priceChangePercent":"-9.013","weightedAvgPrice":"110.36943858","prevClosePrice":"116.50000000","lastPrice":"106.00000000","lastQty":"281.14085000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"106.00000000","askQty":"218.85915000","openPrice":"116.50000000","highPrice":"116.50000000","lowPrice":"106.00000000","volume":"481.51853000","quoteVolume":"53144.92982000","openTime":1643929803923,"closeTime":1644016203923,"firstId":170,"lastId":273,"count":104},{"symbol":"TRXUSDT","priceChange":"-0.03676000","priceChangePercent":"-36.760","weightedAvgPrice":"0.07982987","prevClosePrice":"0.10000000","lastPrice":"0.06324000","lastQty":"1903.00000000","bidPrice":"0.06355000","bidQty":"1888.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.10000000","highPrice":"1.49575000","lowPrice":"0.05800000","volume":"44517.00000000","quoteVolume":"3553.78628000","openTime":1643884562781,"closeTime":1643970962781,"firstId":144,"lastId":210,"count":67},{"symbol":"XRPUSDT","priceChange":"-0.10000000","priceChangePercent":"-16.667","weightedAvgPrice":"0.61590748","prevClosePrice":"0.60000000","lastPrice":"0.50000000","lastQty":"300.00000000","bidPrice":"0.50000000","bidQty":"183.20000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.60000000","highPrice":"3.08800000","lowPrice":"0.50000000","volume":"13689.80000000","quoteVolume":"8431.65024000","openTime":1643926264152,"closeTime":1644012664152,"firstId":332,"lastId":476,"count":145},{"symbol":"BNBBTC","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00991409","prevClosePrice":"0.00993100","lastPrice":"0.00993100","lastQty":"0.31000000","bidPrice":"0.00893100","bidQty":"0.10000000","askPrice":"0.00993100","askQty":"672.80000000","openPrice":"0.00993100","highPrice":"0.00993100","lowPrice":"0.00731000","volume":"103.63000000","quoteVolume":"1.02739693","openTime":1643927113274,"closeTime":1644013513274,"firstId":206,"lastId":231,"count":26},{"symbol":"ETHBTC","priceChange":"0.22787000","priceChangePercent":"315.916","weightedAvgPrice":"0.24303250","prevClosePrice":"0.07214500","lastPrice":"0.30000000","lastQty":"3.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.30000000","askQty":"9.00000000","openPrice":"0.07213000","highPrice":"0.30000000","lowPrice":"0.07213000","volume":"4.00000000","quoteVolume":"0.97213000","openTime":1643884608187,"closeTime":1643971008187,"firstId":8,"lastId":9,"count":2},{"symbol":"LTCBTC","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00000000","prevClosePrice":"0.00287400","lastPrice":"0.00000000","lastQty":"0.00000000","bidPrice":"0.00297700","bidQty":"1.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.00000000","highPrice":"0.00000000","lowPrice":"0.00000000","volume":"0.00000000","quoteVolume":"0.00000000","openTime":1643883202421,"closeTime":1643969602421,"firstId":-1,"lastId":-1,"count":0},{"symbol":"TRXBTC","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00000156","prevClosePrice":"0.00000156","lastPrice":"0.00000156","lastQty":"512.80000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00000156","askQty":"344.70000000","openPrice":"0.00000156","highPrice":"0.00000156","lowPrice":"0.00000156","volume":"512.80000000","quoteVolume":"0.00079996","openTime":1643873130126,"closeTime":1643959530126,"firstId":6,"lastId":9,"count":4},{"symbol":"XRPBTC","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00001627","prevClosePrice":"0.00001627","lastPrice":"0.00001627","lastQty":"158.60000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00001627","askQty":"65.60000000","openPrice":"0.00001627","highPrice":"0.00001627","lowPrice":"0.00001627","volume":"158.60000000","quoteVolume":"0.00258040","openTime":1643873454115,"closeTime":1643959854115,"firstId":10,"lastId":15,"count":6},{"symbol":"LTCBNB","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.30569997","prevClosePrice":"0.29970000","lastPrice":"0.30570000","lastQty":"0.25967000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.30570000","highPrice":"0.30570000","lowPrice":"0.30570000","volume":"0.25967000","quoteVolume":"0.07938111","openTime":1643865673352,"closeTime":1643952073352,"firstId":6,"lastId":6,"count":1},{"symbol":"TRXBNB","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00000000","prevClosePrice":"0.00015750","lastPrice":"0.00000000","lastQty":"0.00000000","bidPrice":"0.00016130","bidQty":"98574.20000000","askPrice":"0.00016150","askQty":"94117.70000000","openPrice":"0.00000000","highPrice":"0.00000000","lowPrice":"0.00000000","volume":"0.00000000","quoteVolume":"0.00000000","openTime":1643924658455,"closeTime":1644011058455,"firstId":-1,"lastId":-1,"count":0},{"symbol":"XRPBNB","priceChange":"0.00000000","priceChangePercent":"0.000","weightedAvgPrice":"0.00163800","prevClosePrice":"0.00162800","lastPrice":"0.00163800","lastQty":"1000.00000000","bidPrice":"0.00000000","bidQty":"0.00000000","askPrice":"0.00000000","askQty":"0.00000000","openPrice":"0.00163800","highPrice":"0.00163800","lowPrice":"0.00163800","volume":"1000.00000000","quoteVolume":"1.63800000","openTime":1643691995629,"closeTime":1643778395629,"firstId":3,"lastId":3,"count":1}]

The key thing to note is the difference, which I think is causing my problems. I am receiving the api result, but for some reason the returnlist function is not returning the result. I feel I am doing something obviously wrong but I’m having trouble figuring it out. I think It may be because bubble thinks I’m returning an array rather than an object

How did you define your return value?

How did you define your custom data type I. The api connector?

Idk how that library works to bubblize JSON bodies. I’d recommend doing this step yourself.

What does the response from the api look like when you request 1 vs all tokens?

@jared.gibb Thanks for your help. I have solved the problem. For anyone else with issues of returning Arrays of objects, my solution is the following code"


const response = context.request(options);
	console.log(response);
    

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

const {convert} = require('json-to-bubble-object');
        
let returnlist = []

if (Array.isArray(body) == false){
returnlist.push(convert(body))
}
    else {
        for (let i = 0; i < body.length; i++) {
  returnlist[i] = convert(body[i]);
}
    }
        
                
    return {
        "result": returnlist,    
    }
}

Essentially, if the returned API data is just one single object, without [], we can pass that through as per the usual. If not, Iterate through each object in the array and use the convert function to include the p prefix to each object. This then allows it to be passed back through the API as a list.

2 Likes

Would love some help returning a value client side from an action - so far this seems impossible!

1 Like