Marc9s
Marc9s

Reputation: 5

Scrape specific value from a json

i'm looking to scrape some stuff from this example, my goal is to scrape just some values and focus on them by matching the title that's inside of the json file (e.g 5.5/6), i would like to let my script pick just values for the section which has as title and option1/2/3 the number 6

({
    "id": 39430269435981,
    "title": "5.5",
    "option1": "5.5",
    "option2": null,
    "option3": null,
    "sku": "195241242248",
    "requires_shipping": true,
    "taxable": true,
    "featured_image": null,
    "available": false,
    "name": "Nike Dunk High 1985 - Lemon Drop / Black / Saturn Gold - 5.5",
    "public_title": "5.5",
    "options": ["5.5"],
    "price": 13108,
    "weight": 1361,
    "compare_at_price": null,
    "inventory_management": "shopify",
    "barcode": null
},
{
    "id": 39430269468749,
    "title": "6",
    "option1": "6",
    "option2": null,
    "option3": null,
    "sku": "195241242255",
    "requires_shipping": true,
    "taxable": true,
    "featured_image": null,
    "available": false,
    "name": "Nike Dunk High 1985 - Lemon Drop / Black / Saturn Gold - 6",
    "public_title": "6",
    "options": ["6"],
    "price": 13108,
    "weight": 1361,
    "compare_at_price": null,
    "inventory_management": "shopify",
    "barcode": null
}

How can i do that? How can i scrape just the id of the part where the title is 6?

Upvotes: 0

Views: 115

Answers (1)

There must be a more specific method in your case, but the generic method is transform this "json" into a list of dicts and iterate through them, example:

import json

jstring = '''
[
{
    "id": 39430269435981,
    "title": "5.5",
    "option1": "5.5",
    "option2": null,
    "option3": null,
    "sku": "195241242248",
    "requires_shipping": true,
    "taxable": true,
    "featured_image": null,
    "available": false,
    "name": "Nike Dunk High 1985 - Lemon Drop / Black / Saturn Gold - 5.5",
    "public_title": "5.5",
    "options": ["5.5"],
    "price": 13108,
    "weight": 1361,
    "compare_at_price": null,
    "inventory_management": "shopify",
    "barcode": null
},
{
    "id": 39430269468749,
    "title": "6",
    "option1": "6",
    "option2": null,
    "option3": null,
    "sku": "195241242255",
    "requires_shipping": true,
    "taxable": true,
    "featured_image": null,
    "available": false,
    "name": "Nike Dunk High 1985 - Lemon Drop / Black / Saturn Gold - 6",
    "public_title": "6",
    "options": ["6"],
    "price": 13108,
    "weight": 1361,
    "compare_at_price": null,
    "inventory_management": "shopify",
    "barcode": null
}
]
'''
jlist = json.loads(jstring)

for jdict in jlist:
    if ( jdict['title'] == "6" ):
        print(jdict['id'])

output

39430269468749

Upvotes: 1

Related Questions