partial-json-parser-js icon indicating copy to clipboard operation
partial-json-parser-js copied to clipboard

Invalid number swallowed without any error

Open sKopheK opened this issue 5 months ago • 4 comments

"text-embedding-3-small": {
      "vector": [1.4564884488666646, 2, 3, 4, 5, .0516156161551515, 7],
      "info": "PROMPT \"representation\" MODEL \"text-embedding-3-small\""
    }

results in

"text-embedding-3-small": {
      "vector": [1.4564884488666646, 2, 3, 4, 5]
    }

how can i get some error reported instead of swallowing syntax error and cutting the rest of valid tokens off within parent?

sKopheK avatar Jul 17 '25 14:07 sKopheK

Thank you for raising this issue! The behavior of partial-json here is indeed not ideal. To be honest, there are a lot of similar problems, but people probably haven't mentioned them because they simply haven't encountered them.

The Python version of this library handles this case more thoroughly—it throws an error when it encounters invalid JSON. I think I can fix this issue in v0.2 of the JavaScript library.

However, I'm quite busy these days, so I'm wondering, is this something you need urgently? Maybe you could consider submitting a PR?

Minimal reproduction

import { parse } from "partial-json"

console.log(parse(`
     [abc
`))

Expected behavior

An error should be thrown instead of returning [].

@sKopheK Do you agree with this expected behavior?

CNSeniorious000 avatar Jul 18 '25 01:07 CNSeniorious000

hello, @CNSeniorious000, thanks for the quick reply. I'm ok with both suggested and current behavior, just need to know that provided JSON is malformed (not considering incomplete of course) - throwing an exception with whatever it parsed might work even though I have probably never seen such combination :).

sKopheK avatar Jul 18 '25 06:07 sKopheK

hi, found another, even more serious one: when there's space between empty array brackets, everything after this gets discarded:

example:

[
{ "id":1,"arr":["hello"]},{"id"     :2    ,    "arr": [        ],"more":"yaya"},{"id":3,"arr":["!"]}
]

returns

[
    {
        "id": 1,
        "arr": [
            "hello"
        ]
    },
    {
        "id": 2,
        "arr": []
    }
]

empty space between curly brackets is fine

sKopheK avatar Jul 21 '25 11:07 sKopheK

Really thank you for discovering this!

I will be attending a hackathon starting tomorrow, but I will make sure to release a new version of this package that addresses these issues no later than July 30th.

CNSeniorious000 avatar Jul 21 '25 21:07 CNSeniorious000