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

118
Views
Renaming JSON subkeys using PHP

I'm trying to help a JSON structure format from an existing project, and the goal is to rename some subkeys not the value. The JSON format like this

{
  "table-name": "Kiwi",
  "created-on": "November 20, 2021",
  "token": "lsUVozOB2TxhvMv",
  "icons": "default",
  "links": "default",
  "extra": "default",
  "mode": "Private",
  "collaborators": [],
  "columns": {
    "Name": {
      "type": "text",
      "extra": ""
    },
    "Info": {
      "type": "longtext",
      "extra": ""
    },
    "Status": {
      "type": "droplist",
      "extra": {
        "fr": "Pending",
        "sc": "On-going",
        "th": "Completed",
        "fo": "Cancelled"
      }
    }
  },
  "data": [{
      "Name": "Team Reports",
      "Info": "Submitting marketing materials reports",
      "Status": "Completed"
    },
    {
      "Name": "Fabia HR",
      "Info": "Brian asking for a report",
      "Status": "Pending"
    },
    {
      "Name": "Fabia",
      "Info": "Meeting with CEO @cafe 9:00",
      "Status": "Cancelled"
    }
  ]
}

And we're looking into the columns to rename the Name into Task and still keep the other. After using array push and slice the keys, I'm getting 0 as the keys and "Task as subkeys like

"0":{
  "Task":{
    "type":"text",
    "extra":""
  },

The code looks like this. Please do share your solutions in your spare time, thank you in advance, really appreciate any attention. Total noob here ๐Ÿ– PHP:

$file = file.json;  
$jsn = file_get_contents($file);
$data = json_decode($jsn, true);
 
//get name data array 
$sub_data = $data['columns']['Name'];

$new_rename = array(
    "Task" => $sub_data;
);

array_push($data['columns'],$new_rename);

//now delete old "Name";
array_slice($data['columns'],'Name')

//save stuff
$jsn = json_encode($data);
file_put_contents($file,$jsn);
over 4 years ago ยท Santiago Trujillo
1 answers
Answer question

0

Given you're working with JSON objects and thus PHP associative arrays (as opposed to numerically indexed), all you really need to do is set the new property and unset the old one

$data["columns"]["Task"] = $data["columns"]["Name"];
unset($data["columns"]["Name"]);

Demo ~ https://3v4l.org/7pBpZ

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!