KDP(電子出版)のメモ 急急如律令

Amazon Kindleダイレクト・パブリッシングでの電子出版や電子書籍の作成販売について、文章やイラストの作成や編集方法について書いています。

PA-API v5のJSONのパース

 PA-API v5のJSONから必要な情報だけを抽出したいと思う。javascriptなんだからオブジェクトに変換すればさっとできそうである。

99nyorituryo.hatenablog.com

 

上の記事を書いてから、PA-APIでの要求を必要な分だけに減らして帰ってくるJSONをパースすることをやろうと思う。JSONはだいぶ減らしてみたが、必要なら後で増やせばいいかな。

 

{ "ItemsResult": { "Items": [ { "ASIN": "4088822048", "BrowseNodeInfo": { "BrowseNodes": [ { "Ancestor": { "Ancestor": { "Ancestor": { "ContextFreeName": "本", "DisplayName": "本", "Id": "465392" }, "ContextFreeName": "ジャンル別", "DisplayName": "ジャンル別", "Id": "465610" }, "ContextFreeName": "コミック・ラノベ・BL", "DisplayName": "コミック・ラノベ・BL", "Id": "466280" }, "ContextFreeName": "コミック", "DisplayName": "コミック", "Id": "2278488051", "IsRoot": false, "SalesRank": 24 }, { "Ancestor": { "Ancestor": { "ContextFreeName": "本", "DisplayName": "本", "Id": "465392" }, "ContextFreeName": "By Publishers", "DisplayName": "By Publishers", "Id": "465614" }, "ContextFreeName": "集英社", "DisplayName": "集英社", "Id": "10805021", "IsRoot": false } ], "WebsiteSalesRank": { "ContextFreeName": "本", "DisplayName": "本", "SalesRank": 24 } }, "DetailPageURL": "https://www.amazon.co.jp/dp/4088822048?tag=99hatena-22&linkCode=ogi&th=1&psc=1", "Images": { "Primary": { "Large": { "Height": 500, "URL": "https://m.media-amazon.com/images/I/51bcIiF+o5L.jpg", "Width": 317 } } }, "ItemInfo": { "ByLineInfo": { "Brand": { "DisplayValue": "集英社", "Label": "Brand", "Locale": "ja_JP" }, "Contributors": [ { "Locale": "ja_JP", "Name": "吾峠 呼世晴", "Role": "著", "RoleType": "author" } ], "Manufacturer": { "DisplayValue": "集英社", "Label": "Manufacturer", "Locale": "ja_JP" } }, "Classifications": { "Binding": { "DisplayValue": "コミック", "Label": "Binding", "Locale": "ja_JP" }, "ProductGroup": { "DisplayValue": "Book", "Label": "ProductGroup", "Locale": "ja_JP" } }, "ExternalIds": { "EANs": { "DisplayValues": [ "9784088822044" ], "Label": "EAN", "Locale": "en_US" }, "ISBNs": { "DisplayValues": [ "4088822048" ], "Label": "ISBN", "Locale": "en_US" } }, "ProductInfo": { "ItemDimensions": { "Height": { "DisplayValue": 6.92912, "Label": "Height", "Locale": "ja_JP", "Unit": "インチ" }, "Length": { "DisplayValue": 4.40944, "Label": "Length", "Locale": "ja_JP", "Unit": "インチ" }, "Width": { "DisplayValue": 0.51181, "Label": "Width", "Locale": "ja_JP", "Unit": "インチ" } }, "ReleaseDate": { "DisplayValue": "2020-02-04T00:00:01Z", "Label": "ReleaseDate", "Locale": "en_US" }, "UnitCount": { "DisplayValue": 1, "Label": "NumberOfItems", "Locale": "en_US" } }, "Title": { "DisplayValue": "鬼滅の刃 19 (ジャンプコミックス)", "Label": "Title", "Locale": "ja_JP" } }, "Offers": { "Listings": [ { "Id": "JpQzg19KtBVWyYFL6FS9tDoK2vVUH2Zqpl9cpOEP3BWhH%2FDTRKSlxzJpQY98JCoIg9hgSF2Ex5KofXXll6d70hXyrqk%2F00HPn7Uvc9wP4wM%3D", "Price": { "Amount": 484, "Currency": "JPY", "DisplayAmount": "¥484" }, "ViolatesMAP": false } ] } } ] } }

 

上のjsonデータをjsonという変数にテキスト形式で代入したとする。そこから必要なデータを取るときに。 Items[0]と深い階層になっているのは、同時に何個かを取得できるのかもしれない。ASIN、URL、出版社、著者、ほんの形式、ISBN、出版日、タイトル、価格。といったものが取れると後で情報を突き合わせやすい気がする。

 

obj = JSON.parse(json);
item=obj.ItemsResult.Items[0]

//英語が思いつかなかったので所々日本語で。

salerank=item.BrowseNodeInfo.WebsiteSalesRank.SalesRank
BrowseNodeId=item.BrowseNodeInfo.BrowseNodes[0].Id
ジャンル名=item.BrowseNodeInfo.BrowseNodes[0].DisplayName
下位ジャンルid=item.BrowseNodeInfo.BrowseNodes[0].Ancestor.Id
下位ジャンル名=item.BrowseNodeInfo.BrowseNodes[0].Ancestor.DisplayName
詳細ジャンルid=item.BrowseNodeInfo.BrowseNodes[0].Ancestor.Ancestor.Id
詳細ジャンル=item.BrowseNodeInfo.BrowseNodes[0].Ancestor.Ancestor.DisplayName
全体カテゴリid=item.BrowseNodeInfo.BrowseNodes[0].Ancestor.Ancestor.Ancestor.Id

全体カテゴリ=item.BrowseNodeInfo.BrowseNodes[0].Ancestor.Ancestor.Ancestor.DisplayName


asin=item.ASIN
url=item.DetailPageURL
imageurl=item.Images.Primary.Large.URL
publisher=item.ItemInfo.ByLineInfo.Manufacturer.DisplayValue
Contributor= item.ItemInfo.ByLineInfo.Contributors[0].Name +item.ItemInfo.ByLineInfo.Contributors[0].Role
Booktype= item.ItemInfo.Classifications.Binding.DisplayValue
isbn=item.ItemInfo.ExternalIds.ISBNs
time=item.ItemInfo.ProductInfo.ReleaseDate.DisplayValue
var today = new Date(time);
ReleaseDate=today
title=item.ItemInfo.Title.DisplayValue
price=item.Offers.Listings[0].Price.Amount