Jonas Fagerlund
Jonas Fagerlund

Reputation: 53

Delete record from JSON file

Trying to delete records from a JSON file but can't get it to work.

The JSON file looks like the following;

[
  {
    "productName": "PlayStation 4",
    "productPrice": "129.5",
    "productUrl": "productUrl.com",
    "trackingUrl": "trackUrl.com",
    "currency": "EUR",
    "programId": 10627777,
    "approvalStatus": 1,
    "ean": "0711719215",
    "sku": "abc123",
    "extraInfo": [
      {
        "key": "color",
        "value": "red"
      }
    ],
    "imageUrl": "store/product_acb.png",
    "inStock": true,
    "manufacturer": "Sony",
    "manufacturerArticleNumber": "1245678",
    "market": "SE",
    "oldPrice": 45,
    "productCategory": "Consoles",
    "productDescription": "PlayStation 4 redefines rich and immersive gameplay with 
     powerful graphics and speed.",
    "shipping": 9
  },
  {
    "productName": "Macbook Pro",
    "productPrice": "1400",
    "productUrl": "site/store/product_acb",
    "trackingUrl": "trackUrl",
    "currency": "EUR",
    "programId": 1062777,
    "approvalStatus": 1,
    "ean": "0711719215",
    "sku": "abc123",
    "extraInfo": [
      {
        "key": "color",
        "value": "silver"
      }
    ],
    "imageUrl": "imgurl.com",
    "inStock": true,
    "manufacturer": "Apple",
    "manufacturerArticleNumber": "12345678",
    "market": "SE",
    "oldPrice": 45,
    "productCategory": "Laptops",
    "productDescription": "The best Macbook yet.",
    "shipping": 6
  }
]

What I want to do is to delete the whole record if it matches one of the blacklisted categories.

The file I'm working with;

import requests


blacklisted_categories = [
    'Laptops'
]

r = requests.post(JSON API response)



def trim_json():
    for resource in r.json():
        if any(ele in resource["productCategory"] for ele in blacklisted_categories):
            del resource

The filtering of categories works but the del resource does not. Am I using the wrong syntax or can you see any other problem?

Upvotes: 1

Views: 203

Answers (2)

Tomalak
Tomalak

Reputation: 338148

I would just create a new list containing all records that are not blacklisted.

import requests

blacklisted_categories = [
    'Laptops'
]

records = requests.post("JSON API URL").json()
new_records = [r for r in records if r['productCategory'] not in blacklisted_categories]

Upvotes: 1

Maroun
Maroun

Reputation: 95948

You can use filter:

filter(lambda x: x['productCategory'] == 'Laptops', r)

r = [{"productName": "PlayStation 4", "productPrice": "129.5", "productUrl": "productUrl.com", "trackingUrl": "trackUrl.com", "currency": "EUR", "programId": 10627777, "approvalStatus": 1, "ean": "0711719215", "sku": "abc123", "extraInfo": [{"key": "color", "value": "red"} ], "imageUrl": "store/product_acb.png", "inStock": True, "manufacturer": "Sony", "manufacturerArticleNumber": "1245678", "market": "SE", "oldPrice": 45, "productCategory": "Consoles", "productDescription": "PlayStation 4 redefines rich and immersive gameplay with powerful graphics and speed.", "shipping": 9 }, {"productName": "Macbook Pro", "productPrice": "1400", "productUrl": "site/store/product_acb", "trackingUrl": "trackUrl", "currency": "EUR", "programId": 1062777, "approvalStatus": 1, "ean": "0711719215", "sku": "abc123", "extraInfo": [{"key": "color", "value": "silver"} ], "imageUrl": "imgurl.com", "inStock": True, "manufacturer": "Apple", "manufacturerArticleNumber": "12345678", "market": "SE", "oldPrice": 45, "productCategory": "Laptops", "productDescription": "The best Macbook yet.", "shipping": 6 } ]

print(list(filter(lambda x: x['productCategory'] == 'Laptops', r)))
>> [{'productName': 'Macbook Pro', 'productPrice': '1400', 'productUrl': 'site/store/product_acb', 'trackingUrl': 'trackUrl', 'currency': 'EUR', 'programId': 1062777, 'approvalStatus': 1, 'ean': '0711719215', 'sku': 'abc123', 'extraInfo': [{'key': 'color', 'value': 'silver'}], 'imageUrl': 'imgurl.com', 'inStock': True, 'manufacturer': 'Apple', 'manufacturerArticleNumber': '12345678', 'market': 'SE', 'oldPrice': 45, 'productCategory': 'Laptops', 'productDescription': 'The best Macbook yet.', 'shipping': 6}]

Or, using list comprehension:

[a for a in r if a['productCategory'] != 'Laptops']

Upvotes: 0

Related Questions