How do I Stringify an object in Bubble?

I’m writing a server-side plugin that returns an object, and after working through the context.async syntax and getting the plugin to return on an object, I now have a new and interesting issue.

I know the plugin returns the object I want because there is a console.log() statement in the plugin that outputs it to the server log.

The console.log() statement is below.

 console.log("CustomPlugin:getLinks:objectToReturn:try3: ", objectToReturn)

And I can see the object from that statement in the server log, as shown below.

However, as the plugin is not fully done, I’m taking baby steps and testing it again after each code addition.

I want to, within Bubble, stringify the object so I can display it in a Text element.

And herein lies the problem.

I’ve experimented with:
Javascript Toolbox - Run javascript
Javascript Toolbox - Server Script
jsoNest Plugin for Bubble

But I’m not figuring out how to address or reference the object correctly.

For example, here is a screenshot of me trying to use Run javascript to simply “console.log()” the object, which in a browser or a Node JS script, just works.
image

The issue is that the object is not text, which of course it isn’t.

For what it’s worth, Bubble did create an object property named “raw body text” that I did not define, and I tried it, but it seems to be always empty.

Does anyone know how to do a

let objectTextRepresentation = JSON.stringify(object)
console.log(objectTextRepresentation)

in Bubble?

You don’t access the whole returned object but you can access its properties. Try “result of step1 propertyX”

There is one text field within the object. If I grab it, I get it.

But I want to see the full object in JSON format.

I could also perform the JSON.stringify() within the plugin and return a JSON object, but Bubble seems “happy” with the object, and I’m not sure how returning a JSON object would affect things.

Do you know?

The Bubble plugin API(s) don’t support arbitrary object publishing. But of course the SSA plugin API essentially returns an object. The individual fields of primitive or app types are what you would return as the fields defined under “Return types”.

Also moved this to “Plugin Builders” in case another plugin builder might want to assist you more.

I thought of a strange way of possibly doing this, and interestingly enough, it worked.

I added a field to returnedObject. It’s _p_stringified.

    let objectToReturn = {
        _p_links: [
            {
                _p_href: "Here ye, here ye, the answer is always 42!",
                _p_rel: "",
                _p_text: ""
            }
        ],
        _p_stringified: "",
        _p_returnStatus: null,
        _p_returnStatusText: ""
    }

Then, after the object is “fully constructed” I ran JSON.stringify() in order to place a stringfiied copy of the object into that field within the object.

The reason I was uncertain if it would work is it seems to weird to place a “snapsnot” of an object into a field of the object the snapshot is being taken off.

 objectToReturn._p_stringified = JSON.stringify(objectToReturn)

But, it worked. I can now visually see the object is complete and intact.

You’re going to have to take this particular hack up with @jared.gibb. I have no idea how they figured out the prefix thing (or who explained that to then), but my jam is making generally useful Bubble plugins for the community, not custom hacks for specific apps which is where your particular use case goes. :man_shrugging:

1 Like

ADDITIONALLY: I can tell you’re just thrashing around. It’s not magic you’re doing here, it’s development. Don’t guess at shit. Go learn it.

This topic was automatically closed after 70 days. New replies are no longer allowed.