I am reading data from S3 bucket using "select_object_content", everything working for me. I can able to fetch result from s3 JSON file. But after getting the result I checked the records and its type printing as a string(ie <class 'str'>), but I cannot able to access values inside that object and it's throwing an error.
Code sample
Sample JSON file attached in S3
query = "SELECT * FROM s3object[*]['domain'][*] r where r.id > " + str(start) + " and r.id <= " + str(stop) + " limit " + str(pagesize);
r = s3.select_object_content(
Bucket=cache,
Key= key + '.json',
ExpressionType='SQL',
Expression= query,
InputSerialization={'JSON': {"Type": "Lines"}},
OutputSerialization={'JSON': {}},
)
for event in r['Payload']:
if 'Records' in event:
records = event['Records']['Payload'].decode('utf-8')
print(type(records)); // <class 'str'>
print(records); // Please see records printing below example
print(records['hostname']) // Throwing error - 3 records are printing together so cannot access first record
Records are printing like this, I want to access values inside this object
{"id":6,"hostname":"amt.in.","subtype":"NS","value":"ns-529.awsdns-02.net.","passive_dns_count":"7"}
{"id":7,"hostname":"amt.in.","subtype":"NS","value":"ns-1288.awsdns-33.org.","passive_dns_count":"6"}
{"id":8,"hostname":"amt.in.","subtype":"NS","value":"ns-1288.awsdns-33.org.","passive_dns_count":"7"}
I tried to parse the string to object like below but it also throwing an error
parsed_json = (json.loads(records))
print(parsed_json.hostname)
Your help is much appreciated. Thank you.
Also tried removing utf-8 encoding then printing records like below
I tried to remove utf-8 encoding now getting some valid errors
Now the record type printing as bytes
<class 'bytes'>
Record is printing like this way
b'{"id":6,"hostname":"amt.in.","subtype":"NS","value":"ns-529.awsdns-02.net.","passive_dns_count":"7"}\n{"id":7,"hostname":"amt.in.","subtype":"NS","value":"ns-1288.awsdns-33.org.","passive_dns_count":"6"}\n{"id":8,"hostname":"amt.in.","subtype":"NS","value":"ns-1288.awsdns-33.org.","passive_dns_count":"7"}\n{"id":9,"hostname":"amt.in.","subtype":"NS","value":"ns-1983.awsdns-55.co.uk.","passive_dns_count":"6"}\n{"id":10,"hostname":"amt.in.","subtype":"NS","value":"ns-1983.awsdns-55.co.uk.","passive_dns_count":"7"}\n'