I am having trouble efficiently storing the PhoneNumber value in the block below
response = requests.get(api)
data = json.loads(response.text)
So after doing the above, I have a dictionary 'data' which looks like this:
{'Key1': 'Some Value ', 'Key2': [{'PhoneNumber': '180000000000'}]}
The goal is to get just the PhoneNumber value i.e. '180000000000'
, and my first reaction was the below which works, but I know this will not scale well. Wondering if within the json.loads call above I can pass an argument or if anyone has other advice on how to more effectively get this value I am interested.
data = data['Key2'][0]['PhoneNumber'])
So after the above data is now = '180000000000'
and I want to store it in s3
, I had old code (below) for a JSON file, and this works but is pointless. I just need to store the phone numbers as text file ( I know how to use with open for .txt but I am confused on how to use the io.Bytes and if it should be SringIO instead of BytesIO)
inmemory = io.BytesIO()
with gzip.GzipFile(fileobj=inmemory, mode='wb') as fh:
with io.TextIOWrapper(fh, encoding='utf-8',errors='replace') as wrapper:
wrapper.write(json.dumps(data, ensure_ascii=False,indent=2))
inmemory.seek(0)
s3_resource.Object(s3bucket, s3path + '.json.gz').upload_fileobj(inmemory)
inmemory.close()
And when it is stored like this, I have when I pull the value from S3, I have to use 'with gzip.open' and decode it ect