I'm trying to query my DynamoDB table and convert the results to a json string.
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("eu-central-1")},
)
dyn = dynamodb.New(sess)
var limit int64 = 5
out, err := dyn.Scan(&dynamodb.ScanInput{
TableName: aws.String("Products"),
Limit: &limit,
})
b, _ := json.Marshal(out.Items[0])
fmt.Println(string(b))
But the result is unfortunately filled with NULLs, and I don't want to send the whole thing over the wire:
"Price":{"B":null,"BOOL":null,"BS":null,"L":null,"M":null,"N":"17119","N
S":null,"NULL":null,"S":null,"SS":null}
I know that this has to do with the dynamo.AttributeValue type. Is it possible to omit null values?
Thank you.
You should be able to do this with JSON struct omitempty tags
type Total struct {
A *ColorGroup`json:",omitempty"`
B string`json:",omitempty"`
}
So I found "an" answer but I don't know if there's a better one.
var p Product
dynamodbattribute.UnmarshalMap(out.Items[0], &p)
b, _ := json.Marshal(p)
fmt.Println(string(b))
First I unmarshal the item from DynamoDB to a Go Struct then marshal to JSON. That seems like a lot of work, I would prefer to go from map[string]*AttributeValue
directly to a JSON string (without all these NULL
values of course)
So if somebody can come up with something more elegant, pls share.