cannot cast to jsonb if non-negative numeric vlaue have plus sign


the following command working

select '[0,1, -2,  -0.3444, 5.6]'::jsonb;

However the following 3 not working.

select '[0,1, -2,  (+0.3444), 5.6]'::jsonb;
select '[0,1, -2,  +0.3444, 5.6]'::jsonb;
select '[0,1, -2,  +0, 5.6]'::jsonb;

The following working.

select +0.1;
select (+0.1)::text;

>Solution :

The first working example is a string containing a valid JSON document being cast as JSONB; the other ones contain valid PostgreSQL arithmetic expressions.

The non-working examples again have strings being cast into JSONB, but the strings contain invalid JSON expressions, and thus cannot be parsed as JSON. If you take a look at JSON grammar, number is integer fraction exponent, and integer can have digits that optionally start with -. The sign + is not allowed in JSON. The parentheses are not allowed, either. You can verify this e.g. using JSON validator.

Leave a ReplyCancel reply