meerkat
meerkat

Reputation: 3003

How to pass arbitrary JSON map to custom type via variable

I am trying to pass custom JSON data from variable to initialize jwt.MapClaims object (type MapClaims map[string]interface{}).

The initialization works by directly passing keys and values:

jwt.MapClaims{
    "key": "value",
    "anotherKey": "anotherValue",
}

I have a function with argument named claimData of type map[string]interface{}

I've accomplished to pass the data and initialize MapClaims this way:

jwtClaims := jwt.MapClaims{}

for key, value := range claimData {
    jwtClaims[key] = value
}

Is it possible to initialize it directly in 1 line without using any iteration?

Upvotes: 3

Views: 97

Answers (1)

meerkat
meerkat

Reputation: 3003

Thanks to @mkopriva's comment, using type conversion, the answer is pretty simple:

jwt.MapClaims(claimData)

Upvotes: 2

Related Questions