-1

I am a newbie when it comes to regex. I have a json string that includes timestamp, I would like to remove all the occurrences of the date field in the string.

the beautified json string looks like this:

{
  "abc": 157,
  "efg": 1,
  "hij": "1",
  "klm": "0.00",
  "created_at": {
    "date": "2020-04-08 12:53:34.682759",
    "timezone_type": 3,
    "timezone": "UTC"
  },
  "updated_at": {
    "date": "2020-04-08 12:53:34.682759",
    "timezone_type": 3,
    "timezone": "UTC"
  }
}

I want to remove the all the occurrences of the string that starts with "date": " and ends with ",

so the output would look like the following:

{
  "abc": 157,
  "efg": 1,
  "hij": "1",
  "klm": "0.00",
  "created_at": {
    "timezone_type": 3,
    "timezone": "UTC"
  },
  "updated_at": {
    "timezone_type": 3,
    "timezone": "UTC"
  }
}

I know preg_match_all can actually help find all matching occurrences, however, i found difficulties in building the pattern especially that my pattern includes commas and double quotes.

Alladin
  • 891
  • 2
  • 23
  • 40
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Apr 08 '20 at 14:13
  • 1
    You will need to remove the date from the source not by using any regex on data values. I suggest you remove the date from the source which generates this JSON or use js by traverse the object. – Sachin Kumar Apr 08 '20 at 14:13
  • @SachinKumar i actually still need the date. I want to use this just for unit test comparison of expected output and actual output. The only difference between outputs is the date field. so i can't unset it from the source generating it. The other issue is that i have many nested values, so to loop through the elements isn't an option i guess. that's why i thought maybe i will just treat it as a string and find all the occurrences of my pattern and remove it. – Alladin Apr 08 '20 at 14:23
  • This question has nothing to do with regex. To edit JSON in PHP use `json_decode`, edit the array, and reencode it. – Casimir et Hippolyte Apr 08 '20 at 14:44

1 Answers1

1

Regex (or any other string function) are not the way to edit a JSON string! You have to decode it to an array, then edit it, to finally reencode it to JSON.

$json = <<<'JSON'
{
  "abc": 157,
  "efg": 1,
  "hij": "1",
  "klm": "0.00",
  "created_at": {
    "date": "2020-04-08 12:53:34.682759",
    "timezone_type": 3,
    "timezone": "UTC"
  },
  "updated_at": {
    "date": "2020-04-08 12:53:34.682759",
    "timezone_type": 3,
    "timezone": "UTC"
  }
}
JSON;

$arr = json_decode($json, true);

function delete_key(&$arr, $key) {
    foreach($arr as $k => &$v) {
        if ( $k === $key ) {
            unset($arr[$k]);
            continue;
        }
        if ( is_array($v) ) {
            delete_key($v, $key);
        }
    }
}

delete_key($arr, 'date');

print_r(json_encode($arr, JSON_PRETTY_PRINT));

demo

Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113
  • thanks you for your answer, but the problem is, my json is not as simple as the example provided, it's a multilevel array(up to 5 levels); so to have a nested loop is a lot of work. that's why i preferred to treat it as a string, unless there is a different way that i'm not aware of. – Alladin Apr 08 '20 at 14:51
  • @Alladin: as you can see, I used a recursive function to locate the keys, so there's no reason this code doesn't work even with more than 5 levels (that is few). Test it. – Casimir et Hippolyte Apr 08 '20 at 14:54