Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda