Home > front end >  Convert DynamoDB JSON to AttributeValue, Go Object or Json
Convert DynamoDB JSON to AttributeValue, Go Object or Json

Time:12-03

I am trying to convert simple DynamoDB Object string:

{
  "Item": {
    "Id": {
      "S": "db31"
    },
    "CreateTime": {
      "N": "1647882237618915000"
    }
}

to either dynamodb.AttributeValue and then map to a go object (go type structure) or convert to a simple JSON go object.

I think, there are similar answers (1, 2, 3) in Java, but I didn't find a similar implementation in Golang.

CodePudding user response:

You could create a struct type and use json.Unmarshal to unmarshal the JSON string like this:

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type Record struct {
    Item struct {
        Id struct {
            S string
        }
        CreateTime struct {
            N string
        }
    }
}

func main() {

    str := `{
  "Item": {
    "Id": {
      "S": "db31"
    },
    "CreateTime": {
      "N": "1647882237618915000"
    }
  }
}`

    var record Record
    if err := json.Unmarshal([]byte(str), &record); err != nil {
        fmt.Fprintf(os.Stderr, "unmarshal failed: %v", err)
        os.Exit(1)
    }

    fmt.Printf("%s %s", record.Item.Id.S, record.Item.CreateTime.N)
}


If you want a different approach, and want to transform the result into a structure that is different than the JSON, you could use a library like gjson.

Here is an example "flattening" the result into a simpler struct:

package main

import (
    "fmt"
    "github.com/tidwall/gjson"
)

type Record struct {
    Id         string
    CreateTime string
}

func main() {

    str := `{
  "Item": {
    "Id": {
      "S": "db31"
    },
    "CreateTime": {
      "N": "1647882237618915000"
    }
  }
}`

    values := gjson.GetMany(str, "Item.Id.S", "Item.CreateTime.N")

    record := Record{
        Id:         values[0].Str,
        CreateTime: values[1].Str,
    }

    fmt.Printf("%s %s", record.Id, record.CreateTime)
}

CodePudding user response:

I hope this is not against the TOS. Please let me know if it is, and I will delete the answer. I tried this question on the OpenAI Chatbot and here is it's answer. Works locally for me [https://chat.openai.com/chat].

Here is an example of how you can convert a DynamoDB object string to a JSON object in Go:

// Import the necessary packages
import (
    "encoding/json"
    "fmt"
)

// Define a struct to represent the DynamoDB object
type DynamoDBObject struct {
    Item struct {
        Id struct {
            S string `json:"S"`
        } `json:"Id"`
        CreateTime struct {
            N string `json:"N"`
        } `json:"CreateTime"`
    } `json:"Item"`
}

func main() {
    // Define the DynamoDB object string
    dynamoDBObjectString := `{
        "Item": {
            "Id": {
                "S": "db31"
            },
            "CreateTime": {
                "N": "1647882237618915000"
            }
        }
    }`

    // Unmarshal the DynamoDB object string into a DynamoDBObject struct
    var dynamoDBObject DynamoDBObject
    json.Unmarshal([]byte(dynamoDBObjectString), &dynamoDBObject)

    // Marshal the DynamoDBObject struct into a JSON object
    jsonObject, _ := json.Marshal(dynamoDBObject)

    // Print the JSON object
    fmt.Println(string(jsonObject))
}

In this example, the DynamoDBObject struct is defined to represent the DynamoDB object, with the fields corresponding to the keys and values in the object. Then, the json.Unmarshal function is used to convert the DynamoDB object string into a DynamoDBObject struct. Finally, the json.Marshal function is used to convert the DynamoDBObject struct into a JSON object, which can then be printed or used as needed.

Here is the output of the code:

    {"Item":{"Id":{"S":"db31"},"CreateTime":{"N":"1647882237618915000"}}}

This is the JSON representation of the DynamoDB object string, with the keys and values of the object represented as fields and values in the JSON object.

  • Related