Home > OS >  Need help reading and updating json file with PHP
Need help reading and updating json file with PHP

Time:03-29

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"
}

CodePudding user response:

  • 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.

CodePudding user response:

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));

}

CodePudding user response:

You can use array_walk_recursive, it will update the array and you can encode it again to JSON :

<?php
$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"}';

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

$arr = json_decode($data, true);
array_walk_recursive($arr, 'my_modifier');
$data = json_encode($arr);
// done, $data is upadted.
  • Related