Sort JSON by field

I have an API call that returns a JSON object with a list of items in alphabetical order. I would like to sort items by a numeric field.

"items": [
      "name": "Apples",
      "revenue": 100,
      "name": "Oranges",
      "revenue": 500,

Is it possible to sort the JSON object in the browser where I would have oranges listed first when I loop through the output?

1 Like

You can use the free Bubble Toolbox plugin to have access to Run Javascript action and Javascript To Bubble element. Add the Javascript to Bubble element to your page and give it a suffix. For this example we could use “example” as the suffix. Then after the api returns the items array, you can use Run Javascript action and add the following code. I will put the “dynamic data” in quotations.

let items = "Result of Step 1's items"

items.sort((a, b) => parseFloat(b.revenue) - parseFloat(a.revenue));


Now you can Set State of a custom state that has a type of “API’s return items” and make the value be Javascript to Bubble Element’s value.


@williamtisdale nice example, especially if you want to do more complex sort.

For your example, you wouldn’t need a plugin. After the API list, :sorted allows you to pick a field to sort by.


@mishav Yes, I see that is all I needed to do here, thank you!

@williamtisdale I may be able to use this to approach another issue I was facing. Thanks for suggesting how to do this using a more sophisticated method.