Thursday, June 8, 2023

How To Convert Escaped Array of Objects String Into Map in Go Lang

Let's assume you have array of objects in escaped string format and you want to convert them to map[string]string we can use the below code format.

package main

import (

type GenericHeader struct {
	Key   string `json:"key"`
	Value string `json:"value"`

func main() {
	escapeJsonString := "[{\"value\":\"123456\",\"key\":\"hash-key\"},{\"value\":\"srinivas4sfdc\",\"key\":\"requesting-host\"},{\"value\":\"token-based-authorization-policy\",\"key\":\"policy-name\"},{\"value\":\"application/x-www-form-urlencoded\",\"key\":\"Content-Type\"}]"
	fmt.Println("Input data:", string(escapeJsonString))
	var headers []GenericHeader
	err := json.Unmarshal([]byte(escapeJsonString), &headers)
	if err != nil {
		fmt.Println("Error:", err)

	headerMap := make(map[string]string)
	for _, header := range headers {
		fmt.Println(header.Key + ".." + header.Value)
		headerMap[header.Key] = header.Value

	fmt.Println("Final Map Data==>",headerMap)

Input Data:


Output Data:

map[requesting-host:srinivas4sfdc hash-key:123456]

Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.

FaceBook Page - I Love Coding. You?

Hope this helps you..Enjoy..!

No comments:

Post a Comment