简体   繁体   中英

Remove \r from JSON response - Python

Following code will query the table and result in a JSON as output

import boto3
import json
import re

def lambda_handler(event, context):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('master')

    response = table.scan()
    data = response['Items']

    while 'LastEvaluatedKey' in response:
        response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
        data.extend(response['Items'])

    return {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Origin' : '*',
        },
        'body': json.dumps(data)
    }

My JSON response contains a \\r.

[
    {
        "r_dt": "29-Oct-18",
        "de_dt": "31-Dec-99\r",
        "v_status": "R",
        "v_num": "M13020"
    },
    {
        "r_dt": "29-Oct-18",
        "de_dt": "31-Dec-99\r",
        "v_status": "R",
        "v_num": "O03873"
    }
}

How do I remove \\r from from my JSON response? I tried JSON.loads/RegEx, but it didn't work

How to remove \\r from from my json reposnse. I tried json.loads/regex , didnt work

Your input data contains \\r so it ends up in JSON response. So, instead of "fixing" JSON output, you have to fix input data. If that's not possible, you have to sanitize data as early as possible, before serializing it to JSON:

def sanitize(item):
    item['de_dt'] = item['de_dt'].rstrip('\r')
    # OR
    item['de_dt'] = item['de_dt'].replace('\r', '')
    return item

# ...

while 'LastEvaluatedKey' in response:
    response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
    data.extend(sanitize(item) for item in response['Items'])
import boto3
import json
import re

def lambda_handler(event, context):
    dynamodb = boto3.resource('dynamodb', region_name='ap-southeast-1')
    table = dynamodb.Table('dsl_vehicle_master')

    response = table.scan()
    data =[sanitize(item) for item in response['Items']]

    while 'LastEvaluatedKey' in response:
        response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
        data.extend(sanitize(item) for item in response['Items'])


    return {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Origin' : '*',
        },
        'body': json.dumps(data)
    }

def sanitize(item):
    item['dereg_dt'] = item['dereg_dt'].rstrip('\r\n')
    # OR
    #item['dereg_dt'] = item['dereg_dt'].replace('\r', '')
    return item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM