Python Forum

Full Version: Return JSON records in single line using python 2.7
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have written a code which takes input from aws Kinesis Stream and loads it into S3 bucket. But it is loading All JSON records in one line, i need each JSON record in a separate line. Below is the code , need help in fixing the code:-

import base64
import json

print('Loading function')


def lambda_handler(event, context):
    print('input format json:',event['records'])
    output = []

    for record in event['records']:
        payload = base64.b64decode(record['data'])
        payload_2 = base64.b64decode(payload)
        sector = json.loads(payload_2)
        sector_dumps = json.dumps({'resourceType': sector['entry'][0]['resource']['resourceType'],
               'status':sector['entry'][0]['resource']['status'],
               'id': sector['entry'][0]['resource']['id'],
               'code': sector['entry'][0]['resource']['code']['coding'],
               'value': sector['entry'][0]['resource']['valueQuantity']['value'],
               'unit' : sector['entry'][0]['resource']['valueQuantity']['unit'],
               'effectiveDateTime':sector['entry'][0]['resource']['effectiveDateTime'],
               'subject':sector['entry'][0]['resource']['subject'],
               'performer':sector['entry'][0]['resource']['performer'],
               'category':sector['entry'][0]['resource']['category'],
              } )
        sector_byte = sector_dumps.encode("utf-8")
        sector_byte_encode = base64.b64encode(sector_byte)
        # Do custom processing on the record payload here
        output_record = {
            'recordId': record['recordId'],
            'result': 'Ok',
            'data': sector_byte_encode
        }
        output.append(output_record)

    print('Successfully processed {} records.'.format(len(event['records'])))
    return {'records': output}