jsonparser icon indicating copy to clipboard operation
jsonparser copied to clipboard

Deleting and Adding a Key corrupts the JSON

Open bandikishores opened this issue 5 years ago • 1 comments

Hi,

If we delete a name & add it back, the JSON is corrupted. Its Partially formed and values at the end of the JSON are truncated.

Code:

package main

import (
	"fmt"
	"github.com/buger/jsonparser"
)

var jsonByteGlobal []byte = []byte(
	`{
		"person": {
		  "fullName": "Leonid Bugaev"
		},
		"company": {
		   "name": "Acme"
		}
	  }`)

func main() {
	jsonByteArray := jsonByteGlobal

	jsonByteArray = jsonparser.Delete(jsonByteArray, "person", "fullName")
	fmt.Printf("After Deletion : %s\n", string(jsonByteArray))

	jsonparser.Set(jsonByteArray, []byte(`"Kishore Bandi"`), "person", "fullName")
	fmt.Printf("After Addition : %s\n", string(jsonByteArray))
}

Output:

After Deletion : {
		"person": {
		},
		"company": {
		   "name": "Acme"
		}
	  }
After Addition : {
		"person": {"fullName":"Kishore Bandi"},
		"company": {

In above output, the company part of the JSON got truncated when we tried to add "person"."fullName"

Playground : https://play.golang.org/p/y4cE3UaGoR7

bandikishores avatar Jun 25 '20 04:06 bandikishores

Thanks for reporting. I'll take a look

AllenX2018 avatar Jun 28 '20 12:06 AllenX2018