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

373
Views
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 answers
Answer question

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 Report

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 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!