Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda