python - How to write JSON data to Dynamodb by ignoring empty elements in boto3 -
i write following data group dynamodb.
there 100 data. since images not required, there mixture , without image_url element.
(questionslist.json)
{ "q_id" : "001", "q_body" : "where capital of united states?", "q_answer" : "washington, d.c.", "image_url" : "/washington.jpg", "keywords" : [ "unitedstates", "washington" ] }, { "q_id" : "002", "q_body" : "where capital city of uk?", "q_answer" : "london", "image_url" : "", "keywords" : [ "uk", "london" ] },
since writing test phase, dynamodb write prepared in localhost:8000 using serverless-dynamodb-local plugin of serverless framework, not production environment.
in order write above json data dynamodb, wrote following code in boto 3 (aws sdk python).
from __future__ import print_function import boto3 import codecs import json dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="http://localhost:8000") table = dynamodb.table('questionlisttable') open("questionlist.json", "r", encoding='utf-8') json_file: items = json.load(json_file) item in items: q_id = item['q_id'] q_body = item['q_body'] q_answer = item['q_answer'] image_url = item['image_url'] keywords = item['keywords'] print("adding detail:", q_id, q_body) table.put_item( item={ 'q_id': q_id, 'q_body': q_body, 'q_answer': q_answer, 'image_url': image_url, 'keywords': keywords, } )
when code executed, following error occurs in null character part.
botocore.exceptions.clienterror: error occurred (validationexception) when calling putitem operation: 1 or more parameter values invalid: attributevalue may not contain empty string
apparently seems caused json 's null character.
if exclude image_url containing null character target of writing below, writing completed without problem.
from __future__ import print_function import boto3 import codecs import json dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="http://localhost:8000") table = dynamodb.table('questionlisttable') open("questionlist.json", "r", encoding='utf-8') json_file: items = json.load(json_file) item in items: q_id = item['q_id'] q_body = item['q_body'] q_answer = item['q_answer'] #image_url = item['image_url'] keywords = item['keywords'] print("adding detail:", q_id, q_body) table.put_item( item={ 'q_id': q_id, 'q_body': q_body, 'q_answer': q_answer, #'image_url': image_url, 'keywords': keywords, } )
since dynamodb nosql, there may other methods make use of characteristics, how correct code write above data ignoring empty characters? "if image_url exists, write if not, ignore it."
thank you.
i solved problem. can set null follows.
from __future__ import print_function import boto3 import codecs import json dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-1', endpoint_url="http://localhost:8000") table = dynamodb.table('questionlisttable') open("questionlist.json", "r", encoding='utf-8_sig') json_file: items = json.load(json_file) item in items: q_id = item['q_id'] q_body = item['q_body'] q_answer = item['q_answer'] image_url = item['image_url'] if item['image_url'] else none keywords = item['keywords'] if item['keywords'] else none print("adding detail:", q_id, q_body) table.put_item( item={ 'q_id': q_id, 'q_body': q_body, 'q_answer': q_answer, 'image_url': image_url, 'keywords': keywords, } )
in order check situation of dynamodb, use offline plugin of serverless framework run api gateway in local environment. when called api using postman, null inserted in value.
{ "q_id" : "001", "q_body" : "where capital of united states?", "q_answer" : "washington, d.c.", "image_url" : "/washington.jpg", "keywords" : [ "unitedstates", "washington" ] }, { "q_id" : "002", "q_body" : "where capital city of uk?", "q_answer" : "london", "image_url" : "null", "keywords" : [ "uk", "london" ] },
Comments
Post a Comment