arraysjsonedit

Deleting an object in an array within JSON based on a name-value pair condition


I have a need to manipulate an incoming JSON file, removing an object in an array, based on a pair condition. If ("color": != 2) delete the object. On Windows env, no Python installed at this point, but would like to know that side too if it's better/only option.

Inside "markers" array I have 4 objects to process: "color": 7,2,2,7. I need the output JSON to have only the full objects containing ("color": 2) present. The objects must be deleted, not empty, because the JSON extraction on new file will not work otherwise.

I've read a few other posts that ask similar, but have not found a way to remove the object itself.

{
  "my_id": "12345678987654321",
  "markers": [
    {
      "color": 7,
      "comments": "",
      "end": {
        "frames": 1,
        "seconds": 0,
        "timecode": "00:00:00;01"
      },
      "guid": "abcdefgh",
      "name": "",
      "start": {
        "frames": 1,
        "seconds": 0,
        "timecode": "00:00:00;01"
      },
      "type": "Comment"
    },
    {
      "color": 2,
      "comments": "",
      "end": {
        "frames": 17938,
        "seconds": 299.265633333333,
        "timecode": "00:05:00;00"
      },
      "guid": "qwertyuiop",
      "name": "",
      "start": {
        "frames": 17938,
        "seconds": 299.265633333333,
        "timecode": "00:05:00;00"
      },
      "type": "Comment"
    },
    {
      "color": 2,
      "comments": "",
      "end": {
        "frames": 34164,
        "seconds": 569.9694,
        "timecode": "00:10:00;00"
      },
      "guid": "zxcvbnm",
      "name": "",
      "start": {
        "frames": 34164,
        "seconds": 569.9694,
        "timecode": "00:10:00;00"
      },
      "type": "Comment"
    },
    {
      "color": 7,
      "comments": "",
      "end": {
        "frames": 88775,
        "seconds": 1481.06291666667,
        "timecode": "00:15:00;00"
      },
      "guid": "lkjhgfdsa",
      "name": "",
      "start": {
        "frames": 88775,
        "seconds": 1481.06291666667,
        "timecode": "00:15:00;00"
      },
      "type": "Comment"
    }
  ],
  "my_media_info": {
    "Audio Channels": "2",
    "Video Height": "720",
    "Video Width": "1280"
    }
}

Desired output would be:

{
  "my_id": "12345678987654321",
  "markers": [
    {
      "color": 2,
      "comments": "",
      "end": {
        "frames": 17938,
        "seconds": 299.265633333333,
        "timecode": "00:05:00;00"
      },
      "guid": "qwertyuiop",
      "name": "",
      "start": {
        "frames": 17938,
        "seconds": 299.265633333333,
        "timecode": "00:05:00;00"
      },
      "type": "Comment"
    },
    {
      "color": 2,
      "comments": "",
      "end": {
        "frames": 34164,
        "seconds": 569.9694,
        "timecode": "00:10:00;00"
      },
      "guid": "zxcvbnm",
      "name": "",
      "start": {
        "frames": 34164,
        "seconds": 569.9694,
        "timecode": "00:10:00;00"
      },
      "type": "Comment"
    }
  ],
  "my_media_info": {
    "Audio Channels": "2",
    "Video Height": "720",
    "Video Width": "1280"
    }
}

Solution

  • Load the file with json, filter the markers list to retain only those items where "color" is 2, then save the file.

    import json
    
    with open('myfile.json') as f:
        data = json.load(f)
    
    data['markers'] = [marker for marker in data['markers'] if marker['color'] == 2]
    
    with open('newfile.json', 'w') as f:
        json.dump(data, f)