简体   繁体   中英

Need help reading and updating json file with PHP

I have about 3000 json files in a folder that I need to edit. I have done some research but everything I have found does not seem to work for my situation. I believe it's because when extracting the data to an array the the data from attributes is in a sub array.

I need to open each file and edit the "trait_type" name. I need to remove Vehicle/ for each trait type.

For example I need to edit

"trait_type": "Vehicle/Exterior"

and change to this.

"trait_type": "Exterior"

Any help would be much appreciated.

JSON file

{
    "name":"2020 Mercedes-Benz Sl550 ",
    "description":"Fully Loaded",
    "image":"http://www.websitename.com/images/2020_mercedes-benz_1.jpg",
    "price":"69900",
    "miles":4500,
    "date":1647530418343,
    "attributes":[
        {
            "trait_type":"Vehicle/Exterior",
            "value":"Black"
        },
        {
            "trait_type":"Vehicle/Interior",
            "value":"Black"
        },
        {
            "trait_type":"Vehicle/Engine",
            "value":"4.7L V8 32V"
        },
        {
            "trait_type":"Vehicle/Fuel",
            "value":"GAS"
        }
    ],
    "Stock Number":"43212"
}
  • Use glob to fetch all JSON filenames
  • For each file, read the content and convert it to stdClass
  • Cycle through attributes , do a simple str_replace on any occurrence of trait_type
  • Convert the stdClass back to JSON string
  • Overwrite each file with the newly replaced content

$files = glob('*.json');
foreach ($files as $file) {
  $json = json_decode(file_get_contents($file));
  foreach($json->attributes as $attrib) {
    if (isset($attrib->trait_type)) {
      $attrib->trait_type = str_replace('Vehicle/', '', $attrib->trait_type);
    }
  }
  file_put_contents($file, json_encode($json, JSON_PRETTY_PRINT));  
}

You should skip JSON_PRETTY_PRINT , just using it for demonstration purposes, so it is easier to see that Vehicle/ is actually removed. Obvious PHP should have write-access to the directory.

Ohh, didn't make in time:D

<?php

if(!is_dir('fixed')){
    mkdir('fixed');
}

foreach(glob('*.json') as $json_file){
    
    $data = json_decode(file_get_contents($json_file));

    foreach($data->attributes as $key => $attribute){

        $data->attributes[$key]->trait_type = preg_replace('/^vehicle\//i', '', $attribute->trait_type);

    }

    file_put_contents('fixed/'.$json_file, json_encode($data, JSON_PRETTY_PRINT));

}

You can usearray_walk_recursive , it will update the array and you can encode it again to JSON (use JSON constants to configure the json_encode behavior):

<?php
// better use a named function if you do it very often
function my_modifier (&$v, $k){
    if ($k == 'trait_type') 
        $v = 'XYZ';
}

$data = '{"name":"2020 Mercedes-Benz Sl550 ","description":"Fully Loaded","image":"http:\/\/www.websitename.com\/images\/2020_mercedes-benz_1.jpg","price":"69900","miles":4500,"date":1647530418343,"attributes":[{"trait_type":"Vehicle\/Exterior","value":"Black"},{"trait_type":"Vehicle\/Interior","value":"Black"},{"trait_type":"Vehicle\/Engine","value":"4.7L V8 32V"},{"trait_type":"Vehicle\/Fuel","value":"GAS"}],"Stock Number":"43212"}';

$arr = json_decode($data, true);
array_walk_recursive($arr, 'my_modifier');
$data = json_encode($arr, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
// done, $data is upadted.
echo "<pre>$data";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM