Forum Academy Marketplace Showcase Pricing Features

Convert Single Input Value's Into JSON Array

I am trying to setup an API integration with PDF Generator API that will create multiple labels for me within a single API call. For an example of what I mean go to the following URL and look at the " Product Labels" example; PDF Generator API | Document templates for automated PDF creation

Description: I would like to take several input values (e.g. Title, Description, Price, Discounted Price, Barcode) and convert them into a JSON array, multiplied by another input value that is a number.

Example #1: I input a Title, Description, Price, Discounted Price and Barcode into their respective inputs and I leave the number input box blank. When I send the API call it will send the following JSON payload and create a single label on the template;

{
    "title": "Label Title",
    "description": "Label Description",
    "price": "8.00",
    "discounted_price": "4.00",
    "barcode": "123456789101"
  }

Example #2: I input a Title, Description, Price, Discounted Price and Barcode into their respective inputs and I set the number input box to 4. When I send the API call it will send the following array in a JSON payload and create four labels on the template;

[
  {
    "title": "Label Title",
    "description": "Label Description",
    "price": "8.00",
    "discounted_price": "4.00",
    "barcode": "123456789101"
  },
  {
    "title": "Label Title",
    "description": "Label Description",
    "price": "8.00",
    "discounted_price": "4.00",
    "barcode": "123456789101"
  },
  {
    "title": "Label Title",
    "description": "Label Description",
    "price": "8.00",
    "discounted_price": "4.00",
    "barcode": "123456789101"
  },
  {
    "title": "Label Title",
    "description": "Label Description",
    "price": "8.00",
    "discounted_price": "4.00",
    "barcode": "123456789101"
  }
]

I am not saving any of this to the DB. They are all coming from the inputs on the page. I am not opposed to saving this information to the DB if need be though. I would simply like to figure out what the best practice would be to take a single text, multiply it by a number input value and send that many of the text value as a JSON array via the API call.

Any help would be greatly appreciated.

Edited from the original post for clarity,

Look at using the :format as text on the list of things you want to include in the JSON

@adamhholmes
I just updated my post to give a more clear picture of what it is I am trying to accomplish, as I felt my original post may not have explained it well enough.

Assuming (as per your post) that the JSON is identical for each item in the list (just duplicated the number of times entered in the input box), then the simplest way is probably to use that number to generate a ‘List of Numbers’ (using the Toolbox Plugin), then use Format as text on that list, and set each item’s values accordingly (from the relevant input values).

1 Like

@adamhholmes
Welp, that did the trick! I had to get a bit creative, but it worked! Thank you thank you!

1 Like