Home > Enterprise >  how would i save specific part of JSON as a variale?
how would i save specific part of JSON as a variale?

Time:11-09

I'm using a an Api to get manga cover arts , and to do so I need to save part of the Json response as a variable.

import requests
import json

title = "prison-school"

base_url = "https://api.mangadex.org"

r = requests.get(
    f"{base_url}/manga",
    params={"title": title}
)

lol  = [manga["id"] for manga in r.json()["data"]]
idManga = lol[0]

r2 = requests.get(
    f"{base_url}/manga/{idManga}?includes[]=cover_art"
)


lol2  = r2.json()

lol3 = json.dumps(lol2)








print(lol3)

this gives me a response of :

{"result": "ok", "response": "entity", "data": {"id": "371a7405-bee1-402c-b0d7-74ea3fb4d587", "type": "manga", "attributes": {"title": {"en": "Prison School"}, "altTitles": [{"ja-ro": "Kangoku Gakuen"}, {"id": "Penjara Di Sekolah"}, {"ru": "\u0428\u043a\u043e\u043b\u0430-\u0422\u044e\u0440\u044c\u043c\u0430"}, {"ja": "\u30d7\u30ea\u30ba\u30f3\u30b9\u30af\u30fc\u30eb"}, {"zh-hk": "\u76e3\u7344\u5b66\u5712"}], "description": {"en": "Hachimitsu Academy, known for its strict academic standards and even stricter school code, is making a giant change this year. In the first time in school history, they are allowing boys to be admitted. As Fujino Kiyoshi starts his first day at Hachimitsu Academy he is shocked to find out that there are only 4 other guys in the entire school, making the ratio of girls to boys 200:1. And to their dismay, not one of the thousand girls will talk to them or even acknowledge them. But Kiyoshi and the guys are about to find out about the shadow student council that has been threatening the female students about interacting with male students  \n\n\n---", "ru": "\u0427\u0430\u0441\u0442\u043d\u0430\u044f \u0416\u0435\u043d\u0441\u043a\u0430\u044f \u0410\u043a\u0430\u0434\u0435\u043c\u0438\u044f \u0425\u0430\u0447\u0438\u043c\u0438\u0442\u0441\u0443 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430 \u0441\u0432\u043e\u0438\u043c\u0438 \u0441\u0442\u0440\u043e\u0433\u0438\u043c\u0438 \u0430\u043a\u0430\u0434\u0435\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430\u043c\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u0432\u043e\u0438\u043c\u0438 \u0441\u0442\u0440\u043e\u0433\u0438\u043c\u0438 \u0448\u043a\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c\u0438. \u0412\u043f\u0435\u0440\u0432\u044b\u0435, \u0437\u0430 \u0432\u0441\u044e \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u044f \u0448\u043a\u043e\u043b\u044b, \u043c\u0430\u043b\u044c\u0447\u0438\u043a\u0438 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u043a \u043f\u043e\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u044e \u0432 \u044d\u0442\u0443 \u0430\u043a\u0430\u0434\u0435\u043c\u0438\u044e. \u0424\u0443\u0434\u0436\u0438\u043d\u043e \u041a\u0438\u0451\u0448\u0438 \u0432 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0447\u0435\u0431\u043d\u044b\u0439 \u0434\u0435\u043d\u044c \u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0432 \u043a\u0443\u043b\u044c\u0442\u0443\u0440\u043d\u044b\u0439 \u0448\u043e\u043a, \u0443\u0437\u043d\u0430\u0432, \u0447\u0442\u043e \u0432 \u0430\u043a\u0430\u0434\u0435\u043c\u0438\u044e \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u043b\u043e \u0442\u043e\u043b\u044c\u043a\u043e 5 \u043f\u0430\u0440\u043d\u0435\u0439, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0447\u0435\u0433\u043e \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u0434\u0435\u0432\u0443\u0448\u0435\u043a \u0438 \u043f\u0430\u0440\u043d\u0435\u0439 - 200:1. \u041f\u0430\u0440\u043d\u0438, \u0442\u0430\u043a \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0432 \u043c\u0430\u043b\u0438\u043d\u0435. \u041d\u043e \u043d\u0435 \u0442\u0443\u0442-\u0442\u043e \u0431\u044b\u043b\u043e. \u041a \u0438\u0445 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u043c\u0443 \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043d\u0438 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0442\u044b\u0441\u044f\u0447\u0438 \u0434\u0435\u0432\u0443\u0448\u0435\u043a \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u0441 \u043d\u0438\u043c\u0438 \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u0438\u0445 \u043a\u0430\u043a \u0441\u0432\u043e\u0438\u0445 \u0441\u043e\u043a\u0443\u0440\u0441\u043d\u0438\u043a\u043e\u0432. \u041a\u0438\u0451\u0448\u0438 \u0438 \u043f\u0430\u0440\u043d\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0440\u0430\u0437\u043e\u0431\u043b\u0430\u0447\u0438\u0442\u044c \u041f\u043e\u0434\u043f\u043e\u043b\u044c\u043d\u044b\u0439 \u0421\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0438\u0439 \u0421\u043e\u0432\u0435\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0433\u0440\u043e\u0436\u0430\u0435\u0442 \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043a\u0430\u043c \u0425\u0430\u0447\u0438\u043c\u0438\u0442\u0441\u0443.  \n  \nWon the Kodansha Manga Award 2013 for best general manga."}, "isLocked": false, "links": {"al": "55297", "ap": "prison-school", "bw": "series/4390", "kt": "18452", "mu": "63043", "amz": "https://www.amazon.co.jp/gp/product/B077JMRFL3", "ebj": "https://ebookjapan.yahoo.co.jp/books/141809/", "mal": "25297", "raw": "https://comic-days.com/episode/10834108156629480592", "engtl": "https://yenpress.com/9781975330248/prison-school-vol-28/"}, "originalLanguage": "ja", "lastVolume": "28", "lastChapter": "277", "publicationDemographic": "seinen", "status": "completed", "year": 2011, "contentRating": "suggestive", "tags": [{"id": "0a39b5a1-b235-4886-a747-1d05d216532d", "type": "tag", "attributes": {"name": {"en": "Award Winning"}, "description": {}, "group": "format", "version": 1}, "relationships": []}, {"id": "423e2eae-a7a2-4a8b-ac03-a8351462d71d", "type": "tag", "attributes": {"name": {"en": "Romance"}, "description": {}, "group": "genre", "version": 1}, "relationships": []}, {"id": "4d32cc48-9f00-4cca-9b5a-a839f0764984", "type": "tag", "attributes": {"name": {"en": "Comedy"}, "description": {}, "group": "genre", "version": 1}, "relationships": []}, {"id": "caaa44eb-cd40-4177-b930-79d3ef2afe87", "type": "tag", "attributes": {"name": {"en": "School Life"}, "description": {}, "group": "theme", "version": 1}, "relationships": []}], "state": "published", "chapterNumbersResetOnNewVolume": false, "createdAt": "2018-02-07T22:21:15 00:00", "updatedAt": "2022-02-15T22:29:07 00:00", "version": 7, "availableTranslatedLanguages": ["es", "ru", "pl", "en", "ar", "fa"], "latestUploadedChapter": "835c358c-7844-452c-bb09-4b6fc6e42c4d"}, "relationships": [{"id": "bb7c8f8b-f82d-4a73-9f87-02713ca82722", "type": "author"}, {"id": "bb7c8f8b-f82d-4a73-9f87-02713ca82722", "type": "artist"}, {"id": "416e58a6-ac84-4f50-92e0-aac6636a2bca", "type": "cover_art", "attributes": {"description": "", "volume": null, "fileName": "e039f18a-eb05-43d1-ac68-598aa09da9c2.jpg", "locale": "ja", "createdAt": "2021-05-24T16:39:34 00:00", "updatedAt": "2021-05-24T16:39:34 00:00", "version": 1}}, {"id": "0924201b-89b1-4a07-82aa-f504eb67d509", "type": "manga", "related": "doujinshi"}, {"id": "1db29ef5-1231-47df-b6a4-3e25182b341c", "type": "manga", "related": "doujinshi"}, {"id": "985f8295-978d-48d4-bc43-4b035e527f77", "type": "manga", "related": "spin_off"}, {"id": "e9fcd6db-1ba2-4140-9813-9028a996ff7c", "type": "manga", "related": "side_story"}]}}

i need to save the "filename" as a variable but every time i do

filename = lol3['filename']

I get this error :

string indices must be integers

CodePudding user response:

Try running filename = lol2['filename']
By using json.dumps you're turning the dictionary back into a json string.

To keep the json format, keep it as a dictionary and access the keys:

import requests
import json

title = "prison-school"

base_url = "https://api.mangadex.org"

r = requests.get(
    f"{base_url}/manga",
    params={"title": title}
)

lol  = [manga["id"] for manga in r.json()["data"]]
idManga = lol[0]

r2 = requests.get(
    f"{base_url}/manga/{idManga}?includes[]=cover_art"
)

lol2  = r2.json()
print(lol2)

CodePudding user response:

The only fileName key is deeply nested in the JSON response. Try this:

import requests

base_url = "https://api.mangadex.org/manga"

(r := requests.get(base_url, params={'title': 'prison-school'})).raise_for_status()

for datum in r.json()['data']:
    (r := requests.get(f"{base_url}/{datum['id']}", params={'includes[]': 'cover_art'})).raise_for_status()
    for rel in r.json()['data'].get('relationships', []):
        if attr := rel.get('attributes'):
            if filename := attr.get('fileName'):
                print(filename)

Output:

e039f18a-eb05-43d1-ac68-598aa09da9c2.jpg
65045cd1-49e3-4594-9d8d-2cc5556a24c9.jpg
543acfb3-032f-49a5-bbde-4d4d04cbadc5.jpg
  • Related