Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
How to dynamically create and iterate Json Object inside a Json Array using Postman - Javascript

I have a payload like mentioned below :

{
  "id": "0001",
  "type": "donut",
  "name": "Cake",
  "ppu": 0.55,
  "batters": {
    "batter": [
      {
        "id": "1001",
        "type": "Regular"
      },
      {
        "id": "1002",
        "type": "Chocolate"
      },
      {
        "id": "1003",
        "type": "Blueberry"
      },
      {
        "id": "1004",
        "type": "Devil’sFood"
      }
    ]
  },
  "topping": [
    {
      "id": "5001",
      "type": "None"
    },
    {
      "id": "5002",
      "type": "Glazed"
    },
    {
      "id": "5005",
      "type": "Sugar"
    },
    {
      "id": "5007",
      "type": "PowderedSugar"
    },
    {
      "id": "5006",
      "type": "ChocolatewithSprinkles"
    },
    {
      "id": "5003",
      "type": "Chocolate"
    },
    {
      "id": "5004",
      "type": "Maple"
    }
  ]
}

**I want to increment the json objects dynamically(it can be a duplicate as well) which is inside the array topping based on the array size. For example if mention the array size as topping[10] it is suppose to create a payload of 10 objects and push those 10 objects of similar type inside the array topping ** Is it possible to dynamically create json objects and post the request in postman??

Kind note : The size of the array should be parameterized. Please let me know. Please find the image highlighted in green. I want to dynamically increase the payload(topping array size based on the index using postman

1

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could do this:

Tab Pre-request

let req = {
    "id": "0001",
    "type": "donut",
    "topping": []
};

let numberOfTopping = 5;

for (let i = 0; i < numberOfTopping; i++) {
    let toppingItem = {
        "id": `${_.random(5001, 5010)}`,
        "type": `${_.sample(["Glazed", "Sugar", "None"])}`
    };
    req.topping[i] = toppingItem;
}

pm.variables.set("req", JSON.stringify(req));

Tab body

enter image description here

Result

{
  "id": "0001",
  "type": "donut",
  "topping": [
    {
      "id": "5006",
      "type": "Glazed"
    },
    {
      "id": "5001",
      "type": "Sugar"
    },
    {
      "id": "5006",
      "type": "Glazed"
    },
    {
      "id": "5006",
      "type": "None"
    },
    {
      "id": "5008",
      "type": "Sugar"
    }
  ]
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!