web-dev-qa-db-ja.com

JSONLファイルをJSONオブジェクトとしてロードする

JSONLファイルをPythonのJSONオブジェクトとしてロードしたいと思います。そうする簡単な方法はありますか?

4
MBT

一般的に、以下のコードはあなたのために働くでしょう:

import json

result = [json.loads(jline) for jline in jsonl_content.split('\n')]

それが応答オブジェクトの場合、結果は次のようになります。

result = [json.loads(jline) for jline in response.read().split('\n')]
5
Andriy Ivaneyko

私のような初心者のためのファイル操作を含む完全なステップ

次のような.jsonlファイルがあると仮定します。

{"reviewerID": "A2IBPI20UZIR0U", "asin": "1384719342", "reviewerName": "cassandra tu \"Yeah, well, that's just like, u...", "helpful": [0, 0], "reviewText": "Not much to write about here, but it does exactly what it's supposed to. filters out the pop sounds. now my recordings are much more crisp. it is one of the lowest prices pop filters on Amazon so might as well buy it, they honestly work the same despite their pricing,", "overall": 5.0, "summary": "good", "unixReviewTime": 1393545600, "reviewTime": "02 28, 2014"}
{"reviewerID": "A14VAT5EAX3D9S", "asin": "1384719342", "reviewerName": "Jake", "helpful": [13, 14], "reviewText": "The product does exactly as it should and is quite affordable.I did not realized it was double screened until it arrived, so it was even better than I had expected.As an added bonus, one of the screens carries a small hint of the smell of an old grape candy I used to buy, so for reminiscent's sake, I cannot stop putting the pop filter next to my nose and smelling it after recording. :DIf you needed a pop filter, this will work just as well as the expensive ones, and it may even come with a pleasing aroma like mine did!Buy this product! :]", "overall": 5.0, "summary": "Jake", "unixReviewTime": 1363392000, "reviewTime": "03 16, 2013"}

このコードは機能するはずです:

import json

with open('./data/my_filename.json', 'r') as json_file:
    json_list = list(json_file)

for json_str in json_list:
    result = json.loads(json_str)
    print("result: {}".format(result))
    print(isinstance(result, dict))

.jsonlファイルについて:
http://jsonlines.org/

6
cryanbhu