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

136
Visualizações
How to add hard coded values into a JSON response

I am trying to use PHP to generate JSON to specifically take a desired shape. Below is a snippet of my PHP code:

      if($con){
    $sql="select * from people";
    $result=mysqli_query($con,$sql);
    if($result){
          header("Content-Type: JSON");
        $i=0;
        while($row = mysqli_fetch_assoc($result)){
            $response[$i]['gender']=$row['gender'];
            $response[$i]['first']=$row['first'];
            
            $i++;
        }
        echo json_encode($response, JSON_PRETTY_PRINT);
    }
}

Here is my current JSON response

  [
   {
      "gender":"male",
      "first":"Angela"
   },
   {
      "gender":"female",
      "first":"Krista"
   }
]

And here is my desired response:

{
    "inputs": [
      {

        "values": {
          "gender": "male",
          "first": "Angela"
        }
      },
      {
        "values": {
          "gender": "female",
          "first": "Krista"
        }
      }
    ]
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Almost, add 'values', then your items then add $response to the items array, also the content type for json is application/json, and you should predefine $response as an array, else your get undefined warning and null value when using it in json_encode if the query is empty.

...
        header("Content-Type: application/json");
        $response = [];
        while($row = mysqli_fetch_assoc($result)){
            $response[]['values'] = [
                'gender' => $row['gender'],
                'first' => $row['first']
            ];
        }
        echo json_encode(['inputs' => $response], JSON_PRETTY_PRINT);
...
about 4 years ago · Juan Pablo Isaza Relatório

0

This way:

if ($con) {
    $sql = "select * from people";
    $result = mysqli_query($con,$sql);
    if ($result) {
        header("Content-Type: JSON");
        $response = [
            "inputs" => []
        ];
        while ($row = mysqli_fetch_assoc($result)){
            $response["inputs"][] = [
                "values" => [
                    'gender' => $row['gender'],
                    'first' => $row['first']
                ]
            ];
        }
        echo json_encode($response, JSON_PRETTY_PRINT);
    }
}
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