close
Skip to content

Deleting and Adding a Key corrupts the JSON #209

Description

@bandikishores

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions