Lets say the data challenge is to extract data from ecommerce site with product ids and pricing information. Thus the schema may look like this:
Field name | Field type |
product_id | INTEGER (REQUIRED) |
price | FLOAT (REQUIRED) |
An example of very basic program (in Python) which just prints hardcoded values in required format looks like this:
import json
data = [
{‘product_id’: ‘iphone_10’, ‘price’: 119.99},
[‘product_id’: ‘iphone_11’, ‘price’: 159.99},
]
print(json.dumps(data))
This would be accepted by platform, but of course the idea that data is real and up to date. Therefore your code rather should fetch HTML content, extract required data and structure in required format.
import json
import requests
response = requests.get(‘http://www.iphones.lt’)
# models and prices stored in HTML table
rg = re.compile('<tr><td>(.*?)<td><td>(.*?)</td></tr>’)
results = rg.findall(response.content)
data = []
for model_str, price_str in results:
data.append({
‘product_id’: model_str,
‘price’: float(price_str)
})
print(json.dumps(data))
The key part is that your last part where you print JSON serialized data into standard output using regular print statement. Don’t forget to serialize it using json.dumps()! Typically, the program shall use json.dumps(data) only once to ensure output in JSON format.
print(json.dumps(data))
Warning: don’f forget to remove your debugging prints as it will result in invalid data.
Tip: Go to section Utils to learn how to make processing even easier.