0

In the following code, I am trying to append every oth element to a JSON file:

title = []  # api_result['search_results']['title']
asin = []  # api_result['search_results']['asin']
link = []  # api_result['search_results']['link']
categories = []  # api_result['search_results']['categories'][0]['name']
image_url = []  # api_result['search_results']['image']
rating = []
rating_total = []
price = []  # apit_result['prices'][0]['value']
top_positive_review = []
top_positive_review_rating = []
top_critical_review = []
top_critical_review_rating = []
ratings_total_filtered = []  # apit_result['']
reviews_total_filtered = []
reviews_total = []
reviews = []

for o in range(len(title)):
    with open("metadata.jsonl", "w+") as outfile:
        entry = {
                'title': title[o],
                'asin': asin[o],
                'link': link[o],
                'categories': categories[o],
                'image_url': image_url[o],
                'rating': rating[o],
                'rating_total': rating_total[o],
                'price': price[o],
                'top_positive_review': top_positive_review[o],
                'top_positive_review_rating': top_positive_review_rating[o],
                'top_critical_review': top_critical_review[o],
                'top_critical_review_rating': top_critical_review_rating[o],
                'ratings_total_filtered': ratings_total_filtered[o],
                'reviews_total_filtered': reviews_total_filtered[o],
                'reviews_total': reviews_total[o],
                'reviews': reviews[o]}

I take that this isn't the proper way of doing this. Basically, I want entries like this in the metadata.jsonl file:

{"title":"some title", "asin":"ABCDEF", ...}
{"title":"another title", "asin":"GHIJKL", ...}
...

Where am I going wrong?

Onur-Andros Ozbek
  • 2,998
  • 2
  • 29
  • 78
  • " I am trying to append every oth element to a JSON file:" Every oth element **of what**? And if you want `o` to mean the distance between elements in a list (**I think**?), then it cannot also mean the numeric values that you get from the `range`. Please read [ask] and [mre] and try to show a **simple but complete** example: what might be in `title`? What might be the value of `o`? Exactly what should the corresponding result be? – Karl Knechtel Nov 13 '22 at 09:18
  • Every oth element of the lists – Onur-Andros Ozbek Nov 13 '22 at 09:25
  • To be clear: you mean that `o` should be an integer that is defined ahead of time, such as `3`; and that would mean that element 0, element 3, element 6 etc. should get used? – Karl Knechtel Nov 13 '22 at 09:27
  • People usually do `for i in range`. I just swapped i with o because its my initial. That;'s it. – Onur-Andros Ozbek Nov 13 '22 at 09:28
  • Okay, so you want every element? Or just what? Please actually **explain a concrete problem**. What happens when you try your existing code? How is that different from what is supposed to happen? – Karl Knechtel Nov 13 '22 at 09:29

1 Answers1

1

Opening the file must be done only once with the with open block, otherwise it opens and closes the file at each iteration.

Thereafter, just wrap at each iteration.

Look at this code:

import json

# I am assuming your data in this form
title = ['title_0', 'title_1']
link = ['link_0', 'link_1']

with open("metadata.jsonl", "w") as file:
    for o in range(len(title)):
        entry = {
            'title': title[o],
            'link': link[o],
        }
        json_object = json.dumps(entry, ensure_ascii=False)  # on line json
        file.write(json_object)
        file.write("\n")

output will be:

{"title": "title_0", "link": "link_0"}
{"title": "title_1", "link": "link_1"}
Giuseppe La Gualano
  • 1,491
  • 1
  • 4
  • 24
  • 1
    This works, but what if I try to fetch `title` key. Won't it just return the first one? – The Myth Nov 13 '22 at 09:26
  • The jsonl was born for other purposes (such as processing an http request by sending one line at a time). Look at its [manifest](https://jsonlines.org/) to clarify your ideas. – Giuseppe La Gualano Nov 13 '22 at 09:34
  • Then the `json` data would be of no use. Since, you won't be able to fetch specific values. – The Myth Nov 13 '22 at 09:36