Parsing RAW JSON best practices?

Take a look at this:

"{\"costs\": {\"Total Cost\": 0.0, \"Delivery Cost\": 0.0, \"Total Discount\": 0.0}, \"variables\": [{\"ID\": 3, \"Store\": 1, \"Type\": 1, \"Individual discount\": 0.0, \"Store discount\": 1.0, \"Individual delivery discount\": 0.0, \"Store delivery discount\": 1.0, \"Quantity\": 1.0}, {\"ID\": 19, \"Store\": 1, \"Type\": 2, \"Individual discount\": 0.0, \"Store discount\": 1.0, \"Individual delivery discount\": 0.0, \"Store delivery discount\": 1.0, \"Quantity\": 1.0}]}"

This is whatā€™s outputted as text and stored in my database. Not an issue. Now I need to parse it and turn each ID into a unique thing in the database. Iā€™ve already brain stormed that this is fact possible with regex - but, is it possible another way? Perhaps a plugin?

To summarize, I need to create an object out of each ID, with each object having the pertaining information relative to its ID. This can be accomplished with Regex, but is there an easier way?

2 Likes

It would be really quick to make a plugin to organize this in a manner then pass it back to bubble all ready to be added to the database. Can you show me the fields for the data type you are hoping to create and I can throw one together. I will make it open source so you can see the code.

4 Likes

Thatā€™s awesome!

Weā€™re looking to store each ID.

Each ID would have the values:
-Store
-Type
-Individual Discount
-Store discount
-Individual delivery discount
-Store delivery discount
-Quantity

Hope this helps! Let me know if you need something else!

1 Like

The plugin is called JSON.parse()

Here is an example:

Here is the editor:

2 Likes

Wow this is very awesome!!

I canā€™t seem to install it on my application - would I be able to pay you?

1 Like

Maybe it just hadnā€™t showed up in the marketplace yet but it is there now.

1 Like

Thanks so much for putting this plugin together. I was looking for something like this last week. I looked at the App Data and the element workflows in the Editor and didnā€™t see how you looped through the rows in the RG to save each entry into the database.

I see use of a custom state (still learning the Bubble use of that), but not how you saved each row. Would I have to use a backend workflow to process the state value? (Iā€™m trying to do something similar for a set of bookmarks. I processed first in regex, which works, but want to try JSON ā€“ but canā€™t figure out creating a separate entry for each item.)

The workflowā€™ ā€˜JSON.parse variable has been retrieved runs for each variable in the JSON object. On the bubble side it appears it is set up to just create one item but that workflow is triggered within a loop so it will occur multiple times at 100 milliseconds apart. On each iteration the values of the states of the element are updated and then the variable retrieved event is triggered.

So the plugin is not looping through the repeating group but it is creating the data that is displayed in the repeating group. If you pass in a string of four variables then ā€˜JSON.parse variable has been receivedā€™ will run four times and four items will display in the repeating group.

You can fork this code into your own plugin and make it more dynamic. For instance if you gave the plugin element like 10 text fields you could just type in whatever the objects keys were named and then reference those in the loop and it can pull data based on the structure of the JSON string you pass in.

1 Like

I appreciate the response. I saw earlier how a single item is being created in the database, but not entirely sure how the loop is integrated. Itā€™s most likely because Iā€™m not yet familiar with how to set up loops in Bubble. (Although I do see the page view + 1 setup; will study further.) Once I find how to fork a Bubble plugin, Iā€™ll try it out for my bookmarking case. Thanks once again for this plugin. (Iā€™m originally a Web developer myself, but still learning how to think and do in Bubble.)

EDIT: The fork button for your plugin seems to be grayed out for me. Maybe I need to upgrade from a free account first.

Hi,

So this isnā€™t customizable for our individual scenarios? I noticed that when I placed the element on the page it has all your custom states shown on your example page. I also donā€™t know how to make my own plugin.

Thanks

If you go to the plugin page and click the ā€˜See the Codeā€™ button, you will be able to see the ā€˜statesā€™ and fields I used and this will give you an idea of what you need to change to match your needs. Once you feel like you know how to change it then click the ā€˜Forkā€™ button on the same page and you will have your own copy of the plugin that you can edit. Just make it ā€˜Privateā€™ on the Settings tab and add your app as an authorized app. I didnā€™t take too much time to explain in detail what everything is doing, but I feel like anyone without any coding experience could be able to look at the finished product and the code to determine what should be changed for their use case.

1 Like

Thank you for this explanation. Now I know how to Fork.

Hi @williamtisdale,

It seems this isnā€™t as easy of a process as it sounded. I now understand what variables/states that I need to change, but I donā€™t understand the forking process. Do I need to use GitHub? Also, the Fork button on the plugin page is disabled. Will you help me out here?

Thanks,
George

Iā€™m not sure about the forking process to be honest. Maybe it would be easier to open the plugin code side by side next to a new plugin and just copy everything over as it isnā€™t much.

Hi @williamtisdale-- first, thanks so much for making JSON.parse() ! I read back through the messages here and saw that you really helped people out.

Iā€™m embarrassed to ask what is probably a basic question, but Iā€™m still feeling my way with Bubble. The question is: where in the Workflow do I specify the input string that the plug-in will parse? I have changed the exposed states and variables to suit my JSON and database structure, but the plug-in never actually gets called in my workflow.

For example, in your example app, the input string is placed in the text box Input A, but I donā€™t see where in the workflow that input is being passed to the JSON.parse() plug-in. I wondered if itā€™s something to do with setting an empty state with value ā€œnoā€ but I donā€™t understand what that should do.

Sorry if Iā€™m missing something super basic, but I wonder if you can point me in the right direction. I hope Iā€™ve explained it clearly.

The conditionals on the JSON.parse() element will use the value of Input A when a custom state is triggered and the state is triggered when the JSON button is clicked.

Looking at that now, I can see how that is confusing.

Thanks so much for coming back to me! I really appreciate it.
Perfect, now the logic is working, and the plug-in does get called.

However Iā€™m getting a weird error:

Hereā€™s the update code. I must have looked 5 times but I canā€™t see where in the plug-in section I need to define akJSON.parseā€¦
Screenshot 2023-06-15 at 17.09.12

Sorry for what are probably very basic questions with obvious answers to more experienced users! Can you know from this where Iā€™m going wrong?

On lines 4 and 15 it should only be called JSON and not akJSON. JSON is a built in variable in Javascript/jQuery so it is not working when the name is changed.

Thatā€™s it! Youā€™re super kind to reply so helpfully.

Plug-in is now working with your variables and data; I just canā€™t get it to work with mine. I think itā€™s because my data is strings and arrays rather than numbers. Iā€™ve changed the Exposed States to Text type rather than Number type, but I have a feeling I may need to change something about the instance.publishState or variable terminology so that it deals correctly with strings. If you have the patience to reply, please help me out of my misery. Iā€™m of course also googling to learn how JSON deals with strings and arrays rather than integers etc, so maybe will figure it out eventually.

Thanks again for your super helpful replies above-- theyā€™ve solved most of the problem already.

OK, Iā€™ve found it: the way the plug-in is constructed, I need to enclose all keys in the JSON input in
{ā€œvariablesā€: [ ā€¦ ]}
All good now. Thanks again @williamtisdale ā€“ I owe you one!