tidwall / jj

JSON Stream Editor (command line utility)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Key duplication bug when setting values with -v flag on dot-escaped JSON paths

darkvertex opened this issue · comments

What version I'm using:

Hi, I'm testing this with jj-1.2.1 on Linux.

Bug description:

I have noticed some strange behaviour where using -v to set a value will result in double output for the same key when using backslashes to escape periods in a JSON "path".

Repro steps:

Consider a little sample.json file with the content below:

{
  "plain_example": "tomato", 
  "dotted.example1": "carrot", 
  "dotted.example2": "eggplant"
}

So far so good. I run jj on it to print it as-is and it prints as expected:

> jj -i sample.json -p
{
  "plain_example": "tomato", 
  "dotted.example1": "carrot", 
  "dotted.example2": "eggplant"
}

First let's validate that the -v flag for editing a document does in fact work. Let's try with the plain_example key...

> jj -i sample.json -v potato "plain_example" -p
{
  "plain_example": "potato", 
  "dotted.example1": "carrot", 
  "dotted.example2": "eggplant"
}

So far so good! plain_example which was "tomato" is now "potato". 👍


Now, let's say I want to edit the key dotted.example1 to the same value. At first, I forget to escape the periods -- which is my bad and not a bug -- and it gives me this output:

> jj -i ./_vscode_rez_config/settings.json -v potato "dotted.example1" -p
{
  "dotted": {
    "example1": "potato"
  }, 
  "plain_example": "tomato", 
  "dotted.example1": "carrot", 
  "dotted.example2": "eggplant"
}

Then I read the README a bit closer and learn that I need to escape them... Ok! No problem, I tell myself... let's try:

> jj -i ./_vscode_rez_config/settings.json -v potato "dotted\.example1" -p
{
  "dotted.example1": "potato", 
  "plain_example": "tomato", 
  "dotted.example1": "carrot", 
  "dotted.example2": "eggplant"
}

Woah, what the..?! Escaping worked as I don't get nesting in the result anymore, but now dotted.example1 exists twice! That's not legal JSON. 💩

Could it be the printing code?? Let's rule it out by writing a file out directly with the -o flag:

> jj -i ./_vscode_rez_config/settings.json -v potato "dotted\.example1" -o /tmp/output.json
> cat /tmp/output.json
{"dotted.example1":"potato",
    "plain_example": "tomato",
    "dotted.example1": "carrot",
    "dotted.example2": "eggplant"
}

Same issue, key appears twice and curiously the output is not identical as with -p, plus it's not formatted as prettily as one would expect! Something is definitely wrong here. 😮

Hope you can find the bug. I really love the tool otherwise! :)