0

When I'm reading JSON data -

Sample query:

SELECT *
FROM
  JSON_TABLE(
          CONCAT('[', '351615,12345678', ']'),
          "$[*]"
          COLUMNS(
              ids BIGINT(20) PATH "$"
              )
      ) AS tt

Error :

"Missing a comma or ']' after an array element." at position 2.

When I remove 00 in front of integer it is giving proper result.

SELECT *
FROM
  JSON_TABLE(
          CONCAT('[', '351615,12345678', ']'),
          "$[*]"
          COLUMNS(
              ids BIGINT(20) PATH "$"
              )
      ) AS tt

Output:

351615
12345678

Can any one suggest me what I'm missing?

mohan111
  • 8,633
  • 4
  • 28
  • 55
  • Answered here: https://stackoverflow.com/questions/27361565/why-is-json-invalid-if-an-integer-begins-with-a-leading-zero – guido Jan 22 '20 at 09:50

2 Answers2

1

You can use JSON_ARRAY instead of concat a string to get a JSON array value:

-- single value
SELECT *
FROM JSON_TABLE(
  JSON_ARRAY('00351615'),
  "$[*]"
  COLUMNS(
    ids BIGINT(20) PATH "$"
  )
) AS tt

-- multiple values
SELECT *
FROM JSON_TABLE(
  JSON_ARRAY('00351615', '12345678'),
  "$[*]"
  COLUMNS(
    ids BIGINT(20) PATH "$"
  )
) AS tt

... or you escape the value with ":

-- single value
SELECT *
FROM JSON_TABLE(
  CONCAT('[', '"00351615"', ']'),
  "$[*]"
  COLUMNS(
    ids BIGINT(20) PATH "$"
  )
) AS tt

-- multiple values
SELECT *
FROM JSON_TABLE(
  CONCAT('[', '"00351615","12345678"', ']'),
  "$[*]"
  COLUMNS(
    ids BIGINT(20) PATH "$"
  )
) AS tt

You can also check if your JSON value is valid, using JSON_VALID:

SELECT JSON_VALID(CONCAT('[', '00351615', ']')) -- 0 (not valid)

demo on dbfiddle.uk

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
1

Try below query, it is working:

SELECT * FROM JSON_TABLE('[{"x":"00351615","y":"2"}]',"$[*]" COLUMNS(xval VARCHAR(100) PATH "$.x",yval VARCHAR(100) PATH "$.y")) AS  tt;

Data type should be varchar instead of bigint as value with '00' prefix can not be stored in bigint.

Naveen Kumar
  • 1,988
  • 1
  • 7
  • 11