Skip to content

Flattening out dynamodbattribute.MarshalMap results #2756

Closed
@nathannaveen

Description

Acknowledgements

Describe the bug

We need a way to convert an a raw json to an object like this []map[string]types.attributevalue using the attributevalue package.

I am trying to use dynamodbattribute.MarshalMap to accomplish this. But, when I do this, the maps aren't flattened, and our usage requires the maps flattened because we're querying dynamo using a step function and passing the results into a lambda.

Here is an example:

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

type Item struct {
	Title       string                 `json:"title"`
	Rating      float64                `json:"rating"`
	NestedField map[string]interface{} `json:"nestedField"`
}

func main() {
	item := Item{
		Title:  "test1",
		Rating: 8.5,
		NestedField: map[string]interface{}{
			"key1": "value1",
			"key2": map[string]interface{}{
				"key3": "value2",
			},
		},
	}

	av, err := dynamodbattribute.MarshalMap(item)
	if err != nil {
		log.Fatalf("Got error marshalling map: %s", err)
	}

	var avStr string
	for k, v := range av {
		avStr += fmt.Sprintf("%s:%+v ", k, v)
	}

	fmt.Println(strings.TrimSpace(avStr))
}

Is there already way to flatten out the maps? Or is there a work around for this?

Expected Behavior

We would like the maps to be flattened out because we're querying dynamo using a step function and passing the results into a lambda.

Current Behavior

dynamodbattribute.MarshalMap outputs maps that aren't flattened.

Reproduction Steps

Here's an example:

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

type Item struct {
	Title       string                 `json:"title"`
	Rating      float64                `json:"rating"`
	NestedField map[string]interface{} `json:"nestedField"`
}

func main() {
	item := Item{
		Title:  "test1",
		Rating: 8.5,
		NestedField: map[string]interface{}{
			"key1": "value1",
			"key2": map[string]interface{}{
				"key3": "value2",
			},
		},
	}

	av, err := dynamodbattribute.MarshalMap(item)
	if err != nil {
		log.Fatalf("Got error marshalling map: %s", err)
	}

	var avStr string
	for k, v := range av {
		avStr += fmt.Sprintf("%s:%+v ", k, v)
	}

	fmt.Println(strings.TrimSpace(avStr))
}

Possible Solution

No response

Additional Information/Context

No response

AWS Go SDK V2 Module Versions Used

github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.14.10

Compiler and Version used

go version go1.22.4 darwin/arm64

Operating System and version

macOS Sonoma Version 14.4

Metadata

Assignees

Labels

guidanceQuestion that needs advice or information.p3This is a minor priority issueresponse-requestedWaiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions