Summary
Using modify() to remove an element from an array can produce invalid JSON: string elements leave an extra " behind, and numeric elements get concatenated.
Reproduction steps
- Create the following
test.js:
const { modify, applyEdits } = require('jsonc-parser');
function case1() {
const text = `{"items":["1","2"]}`;
const edits = modify(text, ["items", 1], undefined, {});
console.log('edits case1:', edits);
console.log('result case1:', applyEdits(text, edits));
}
function case2() {
const text = `{"items":[1,2]}`;
const edits = modify(text, ["items", 1], undefined, {});
console.log('edits case2:', edits);
console.log('result case2:', applyEdits(text, edits));
}
case1();
case2();
- Install and run:
npm init -y
npm install jsonc-parser@3.3.1
node test.js
Expected behavior
- case1 output:
{"items":["1"]}
- case2 output:
{"items":[1]}
Actual behavior
- case1 output:
{"items":["1""]} (an extra " remains)
- case2 output:
{"items":[12]} (numbers are concatenated)
Environment
- jsonc-parser: 3.3.1
- Node.js: v22.16.0
- OS: Windows 11
Additional information
- case1 edits:
[{offset: 13, length: 3, content: ''}]
- case2 edits:
[{offset: 11, length: 1, content: ''}]
Summary
Using
modify()to remove an element from an array can produce invalid JSON: string elements leave an extra"behind, and numeric elements get concatenated.Reproduction steps
test.js:Expected behavior
{"items":["1"]}{"items":[1]}Actual behavior
{"items":["1""]}(an extra"remains){"items":[12]}(numbers are concatenated)Environment
Additional information
[{offset: 13, length: 3, content: ''}][{offset: 11, length: 1, content: ''}]