Search code examples
goflatten

how to Flatten json null to empty string in golang


I am searching for go library or work around to flatten the null json value to empty string (""),
from

`{
    "foo": {
            "jim":null
    }
}`  

to

map[foo.jim:""]  

as of now its being ignored for my use case.
can anybody help me with this.

example code https://go.dev/play/p/9hnMEa6QA2O
you can see that i get the output

map[fee:bar]  

but i want

map[foo.jim:"" fee:bar]

Solution

  • after going through the code,
    had to check for nil instead of ignoring it in switch case.

    default:
            if v == nil {
                flatMap[newKey] = ""
            } else {
                flatMap[newKey] = fmt.Sprintf("%v", v)
            }