Skip to content
Advertisement

Invalid JSON Text – SQL (Java JSON Object)

I’m putting in JSON into an SQL table (type: json) and it claims something is wrong – but I can’t tell what it’s pointing out directly.

Current json I’m trying to input:

'{"{"type":"HUMAN"}":"1.0"}'

Yes, it is “json within json” but the code I’m writing is kind of dependent on it being that way. I’m not willing to really change that part.

Here’s the error:

#3140 - Invalid JSON text: "Missing a colon after a name of object member." at position 5 in value for column 'mines.materials'.

If you need code, I have it. I really need to know whats wrong with the JSON, if it requires a fundamental change to the code I can provide what I have.

Advertisement

Answer

You should really try validating some basic assumptions.

'{"{"type":"HUMAN"}":"1.0"}' is not a valid JSON string as you can see below

"use strict";
(function () {
  try {
    JSON.parse('{"{"type":"HUMAN"}":"1.0"}');
  } catch (e) {
    console.error(e);
  }
}());

You are using attempting to use a JSON encoded string as a property key in another JSON encoded string but have not escaped it correctly.

You need to use \" instead of " to escape the quotes

"use strict";
(function() {
  try {
    console.log(JSON.parse('{"{\"type\":\"HUMAN\"}":"1.0"}'));
  } catch (e) {
    console.error(e);
  }
}());
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement