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

375
Visualizações
How to remove the unwanted nested keys from JSON

This is my json:

{  
   "all_counts_reports":{  
      "26":{  
         "name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
      },
      "28":{  
         "name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
      }
   }
}

I want to remove the second level keys (e.g "26:" and "28":) using PHP.

In other words, I want to replace the double-quoted number keys with zero-indexed numeric keys.

How can I make it look like this:

{"all_counts_reports":
    [
        {"name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
        },
        {"name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
        }
    ]
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Here is the demo.

Code:

// declare $json
$array=json_decode($json,true);  // decode as array
// overwrite subarray with zero-indexed keys while preserving subarray values
$array['all_counts_reports']=array_values($array['all_counts_reports']);
var_export(json_encode($array));  // return to json

Input:

$json='{  
   "all_counts_reports":{  
      "26":{  
         "name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
      },
      "28":{  
         "name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
      }
   }
}';

Output:

'{"all_counts_reports":
    [
        {"name":"kumar",
         "date":"2017-04-27",
         "trips_per_day":"2",
         "cash_trips":"0",
         "credit_trips":"1",
         "compliment_trips":"1"
        },
        {"name":"kumar",
         "date":"2017-04-29",
         "trips_per_day":"1",
         "cash_trips":"1",
         "credit_trips":"0",
         "compliment_trips":"0"
        }
    ]
}'

In your javascript, use JSON.parse() to strip the wrapping single quotes:

var str='{"all_counts_reports":[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]}';
var json=JSON.parse(str);
console.log(json);


And because we are running with assumptions, if you want to remove the all_counts_reports key as well, you can use this one-liner:

Code:

$new_json=json_encode(array_values(current(json_decode($json,true))));

Output:

'[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]'
over 4 years ago · Santiago Trujillo Relatório

0

$array = json_decode($your_json_string,true);
print_r($array);
$result = array();
foreach($array['all_counts_reports'] as $rep){
    $result['all_counts_reports'][] = array(
        "name"=>$rep['name'],
        "date"=>$rep['date'],
        "trips_per_day"=>$rep['trips_per_day'],
        "cash_trips"=>$rep['cash_trips'],
        "credit_trips"=>$rep['credit_trips'],
        "compliment_trips"=>$rep['compliment_trips'], 
    );
}

$result_json = json_encode($result);
echo $result_json;

There may be better solution, but this one is right now in my mind

Its unclear that what you looking for, if you just want to remove and dont want to maintain as original json then you can do like this example. But if you dont want your all_counts_reports treated as array [] then this example will not help.

And simply pass to android then above will work.

over 4 years ago · Santiago Trujillo 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