By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: How I Merged and Validated Two JSON Files in Go | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > How I Merged and Validated Two JSON Files in Go | HackerNoon
Computing

How I Merged and Validated Two JSON Files in Go | HackerNoon

News Room
Last updated: 2025/06/24 at 3:51 PM
News Room Published 24 June 2025
Share
SHARE

When I started learning Go, my first real task was to merge two JSON files and validate the result. It sounded easy. It turned out to be a great way to learn how Go handles file I/O, JSON, maps, and type safety.

Here’s how I did it, what I learned, and some alternatives I found along the way.

The Task

  1. Read two JSON files.
  2. Merge them into a single structure.
  3. Validate the result to make sure certain fields exist and are of the correct type.
  4. Write the final JSON to a new file.

Step 1: Reading the JSON Files

Go has great support for reading files and decoding JSON using only the standard library.

data1, _ := ioutil.ReadFile("file1.json")
data2, _ := ioutil.ReadFile("file2.json")

var json1 map[string]interface{}
var json2 map[string]interface{}

json.Unmarshal(data1, &json1)
json.Unmarshal(data2, &json2)

Note: In newer versions of Go, you can use os.ReadFile instead of ioutil.ReadFile, since ioutil is deprecated.

Step 2: Merging the JSON Maps

I needed a function to merge two maps. If a key existed in both maps, the value from the second map should overwrite the first. If the value was a nested object, I needed to merge it recursively.

Here’s the function I wrote:

func mergeMaps(m1, m2 map[string]interface{}) map[string]interface{} {
    for k, v2 := range m2 {
        if v1, ok := m1[k]; ok {
            if v1Map, ok := v1.(map[string]interface{}); ok {
                if v2Map, ok := v2.(map[string]interface{}); ok {
                    m1[k] = mergeMaps(v1Map, v2Map)
                    continue
                }
            }
        }
        m1[k] = v2
    }
    return m1
}

This handled both flat and nested JSON structures correctly.

Step 3: Validating the JSON

Next, I wrote a simple validation function to check that certain fields were present and of the expected type. Since I wasn’t using a schema validator, I had to do manual checks.

func validateJSON(data map[string]interface{}) error {
    if _, ok := data["name"].(string); !ok {
        return fmt.Errorf("missing or invalid 'name' field")
    }

    if port, ok := data["port"]; !ok || reflect.TypeOf(port).Kind() != reflect.Float64 {
        return fmt.Errorf("missing or invalid 'port' field")
    }

    return nil
}

Note: When decoding JSON into interface{}, numbers are treated as float64 by default, which can be confusing at first.

Step 4: Writing the Result

Finally, I wrote the merged and validated JSON back to a file using pretty formatting.

resultBytes, _ := json.MarshalIndent(merged, "", "  ")
ioutil.WriteFile("merged_output.json", resultBytes, 0644)

It worked exactly as expected.

Alternatives: Using a Package to Merge JSON

, I discovered some packages make JSON merging easier. These are a few good ones:

1. mergo

mergo is a popular library for merging maps and structs in Go. It supports nested merges and lets you choose whether to overwrite values.

Install:

go get github.com/imdario/mergo

Example:

var a map[string]interface{}
var b map[string]interface{}

json.Unmarshal([]byte(`{"config": {"debug": false}}`), &a)
json.Unmarshal([]byte(`{"config": {"debug": true, "port": 8080}}`), &b)

mergo.Merge(&a, b, mergo.WithOverride)

Use this if you want clean, deep merging without writing recursive code.

2. json-patch (go-patch)

json-patch lets you apply JSON Patch operations. It’s more for diffing and applying structured updates, but still useful for working with JSON dynamically.

Install:

go get github.com/evanphx/json-patch

Use this if you’re working with patches or want to programmatically update JSON with control.

3. mapstructure

mapstructure helps decode dynamic maps into strongly typed structs. It’s not a merge tool by itself but works well after you merge JSON data and want to bind it to a Go struct.

Install:

go get github.com/mitchellh/mapstructure

Use this when you want structure and type safety after parsing JSON.

Final Thoughts

This task helped me learn a lot about how Go handles data, how JSON works under the hood, and why strong typing matters. Writing a merge function gave me control, but using libraries like mergo made the code more maintainable later.

If you’re learning Go, I recommend starting with small tasks like this. You’ll get real experience with files, encoding, maps, and error handling, all while solving a practical problem.

It’s a small project, but one that taught me a lot.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article From Piano Man to Madison Square Garden: New Billy Joel HBO doc tracks his remarkable career
Next Article FiiO KB3 Review
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

iOS 26 offers massive battery upgrade, but there’s a catch
News
Starbucks denies reports it plans to fully exit China · TechNode
Computing
Researchers beamed light through a man’s head for the first time
News
Eventual launches with $30M in funding to streamline multimodal data processing – News
News

You Might also Like

Computing

Starbucks denies reports it plans to fully exit China · TechNode

1 Min Read
Computing

North Korea-linked Supply Chain Attack Targets Developers with 35 Malicious npm Packages

4 Min Read
Computing

Chinese bubble tea chain Heytea enters America · TechNode

1 Min Read
Computing

VW’s JV to hire 300 workers with Horizon Robotics · TechNode

4 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?