Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array of json objects from for loops

Im attempting to extract values from an html and then convert them into a json array, and so far I have been able to get what I want, but only as separate strings:

I did two for loops:

for line in games_html.findAll('div', class_="product_score"):
  score= ("{'Score': %s}" % line.getText(strip=True))
  print score

for line in games_html.findAll('a'):
  title= ("{'Title': '%s'}" % line.getText(strip=True))
  print title

Which produce these two outputs:

{'Title': 'Uncanny Valley'}
{'Title': 'Subject 13'}
{'Title': '2Dark'}
{'Title': 'Lethal VR'}
{'Title': 'Earthlock: Festival of Magic'}
{'Title': 'Knee Deep'}
{'Title': 'VR Ping Pong'}

and

{'Score': 73}
{'Score': 73}
{'Score': 72}
{'Score': 72}
{'Score': 72}
{'Score': 71}
{'Score': 71}

(they are longer but you can get an idea with this...)

How can I use python to create a json array out of these that would look like:

[{'Title': 'Uncanny Valley', 'Score': 73}, {....}]

I am gonna use the resulting array to do other things afterwards....

Do I need to store the items from the loop into lists and then merge them? Could you please illustrate an example given my scenario?

Help is much appreciated, this is a really cool learning experience for me as I have only used bash until now. Python looks way sexier.

like image 579
geekiechic Avatar asked Mar 17 '17 18:03

geekiechic


People also ask

How do you create an array of objects in JSON?

jsonObject. put("key", "value"); Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.

How do you convert JSON to an array?

Approach 1: First convert the JSON string to the JavaScript object using JSON. Parse() method and then take out the values of the object and push them into the array using push() method.

How do you create an array of JSON in Python?

In Python, you can create JSON string by simply assigning a valid JSON string literal to a variable, or convert a Python Object to JSON string using json. loads() function.


1 Answers

You need to maintain two lists for scores and titles and append all the data to those lists, instead of printing, and then zip those lists along with list comprehension to get the desired output as :

import json
scores, titles = [], []
for line in games_html.findAll('div', class_="product_score"):
    scores.append(line.getText(strip=True))

for line in games_html.findAll('a'):
    titles.append(line.getText(strip=True))

score_titles = [{"Title": t, "Score": s} for t, s in zip(titles, scores)]
print score_titles
# Printing in JSON format
print json.dumps(score_titles)
like image 168
ZdaR Avatar answered Sep 27 '22 22:09

ZdaR