Skip to content

Calling Set on nested JSON object returns invalid JSON after setting value #218

Description

@karanssh

If I call set on a deep nested JSON, the value after it returns misses the quotes required for a valid JSON

Input JSON:

{
		
			  "ipv4":{
				 "name":"testDependency",
				 "source":{
					"address":{
					   "group":"testDepenffdency",
					   "ff":"aa"
					},
					"port":{
					   "any":true
					}
				 },
				 "uuid":"653e5654-b56b-47c1-b4e5-206acbb28f7a"
			  }
	 }

code invoked:

keysList := []string{"ipv4", "source", "address", "group"}
	setVal := []byte(`KaranTest`)
	outputVal, err := jsonparser.Set([]byte(buggedString), setVal, keysList...)

Output:

	 {

		"ipv4":{
			   "name":"testDependency",
			   "source":{
					  "address":{
						 "group":KaranTest,
						 "ff":"aa"
					  },
					  "port":{
						 "any":true
					  }
			   },
			   "uuid":"653e5654-b56b-47c1-b4e5-206acbb28f7a"
		}
}

Here's an example of the problem: https://play.golang.org/p/YT3Ttz0Irib

However, if I pass :

setVal := []byte(`"KaranTest"`)

It works as expected. Most of the time in programs we don't get strings like that. Is this by design? 😊 I'll have to wrap strings around " " to get around this issue.

Activity

  1. changed the title [-]Calling Set on nest JSON object returns invalid JSON after setting value[/-] [+]Calling Set on nested JSON object returns invalid JSON after setting value[/+] on Nov 29, 2020
  2. miscomputer commented on Feb 15, 2021

    @miscomputer

    yes,I found it, too

  3. soulmachine commented on Mar 25, 2021

    @soulmachine

    I got this issue today

  4. blake86 commented on May 25, 2021

    @blake86

    I found this too, any update?

  5. wmz2014 commented on Sep 5, 2022

    @wmz2014

    I get this issue too. How to deal with it?

  6. wmz2014 commented on Sep 5, 2022

    @wmz2014

    I found a way to deal with it
    image

  7. buger commented on Jul 28, 2026

    @buger
    Owner

    Implemented in v1.4.0 — use SetString(data, val, keys...) instead of Set(data, []byte(val), keys...). SetString auto-quotes the value so the output is always valid JSON. See: https://github.com/buger/jsonparser/releases/tag/v1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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