Home > other >  AWS S3. Append data in JSON. JSON file visual format
AWS S3. Append data in JSON. JSON file visual format

Time:10-26

I try to add data to existing in S3 bucket JSON file. Initial structure:

{ 
           "id": "1", 
           "name": "test1",
           "limit": [20.0, 30.0]
           }

I add

{
       "id": "2", 
       "name": "test2", 
       "limit": [10.0, 20.0]
} 

and rewrite it on bucket. When I download it from bucket it get the next structure ""\"{\\\"id\\\": \\\"1\\\", \\r\\n\\\"name\\\"

How can I reformat it in JSON format?

s3 = boto3.client('s3')
bucket = 'bucket'

local_data = {"id": "123", "name": "XYZ", "transaction": [20.0, 30.0]}

local_data_json = json.dumps(local_data)
resp=s3.get_object(Bucket=bucket, Key='key.json')
data=resp.get('Body').read()

json_data = json.dumps(data.decode('utf-8'))
p = json_data   local_data_json

s3.put_object(Bucket=bucket, Key='key.json', 
 Body=json.dumps(p).encode())

CodePudding user response:

Your code is doing everything with strings, rather than converting strings into objects.

The json.dumps(obj) command converts a Python object into a JSON string.

To convert a JSON string into a Python object, use json.loads(str).

Therefore, your code should be doing something like this (I didn't test it):

s3 = boto3.client('s3')
bucket = 'bucket'

local_data = {"id": "123", "name": "XYZ", "transaction": [20.0, 30.0]}

resp=s3.get_object(Bucket=bucket, Key='key.json')
data=resp.get('Body').read()

json_data = json.loads(data.decode('utf-8'))  # Changed
p = json_data   local_data                    # Changed

s3.put_object(Bucket=bucket, Key='key.json', 
 Body=json.dumps(p).encode())
  • Related