Skip to content

realjf/mergejson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mergejson

merge two JSON strings into one

Example

 // merge map: same key name but different value
 a, err := mergejson.MergeJson([]byte(`{"user":{ "name": "hello" }}`), []byte(`{"user":{ "name": "world" }}`))
 if err != nil {
  t.Fatal(err)
  return
 }
 fmt.Printf("same key name but different value: %s\n", a)

 // merge map: same map but a different number of keys(src < dst)
 a1, err := mergejson.MergeJson([]byte(`{"user":{ "name": "hello" }}`), []byte(`{"user":{ "name": "world", "age": 12 }}`))
 if err != nil {
  t.Fatal(err)
  return
 }
 fmt.Printf("same key name but a different number of keys(src < dst): %s\n", a1)

 // merge map: same key name but a different number of keys(src > dst)
 a2, err := mergejson.MergeJson([]byte(`{"user":{ "name": "hello", "age": 12}}`), []byte(`{"user":{ "name": "world" }}`))
 if err != nil {
  t.Fatal(err)
  return
 }
 fmt.Printf("same key name but a different number of keys(src > dst): %s\n", a2)

 // merge slice
 a3, err := mergejson.MergeJson([]byte(`{"user":{ "name": "hello", "lucky_num": [1,2,3,4]}}`), []byte(`{"user":{ "name": "world", "lucky_num": [5,6,7]}}`))
 if err != nil {
  t.Fatal(err)
  return
 }
 fmt.Printf("same key name but a different number of keys(src > dst): %s\n", a3)

About

merge two JSON strings into one

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published