debugsqlMinor
JSON array format in MySQL not working?
Viewed 0 times
formatarrayworkingmysqljsonnot
Problem
What is the proper way to do a JSON data array in MySQL?
Following the JSON.org example http://json.org/example.html does not work:
ERROR 3140 (22032): Invalid JSON text: "The document root must not follow by other values." at position 36 in value for column 'zz_TEST_ObsJSON.jsonData'.
For giggles, tried putting it all in another { }:
ERROR 3140 (22032): Invalid JSON text: "Missing a name for object member." at position 1 in value for column 'zz_TEST_ObsJSON.jsonData'.
This other method that kind of looks like geoJSON does not either:
ERROR 3140 (22032): Invalid JSON text: "Missing a comma or ']' after an array element." at position 10 in value for column 'zz_TEST_ObsJSON.jsonData'.
ERROR 3140 (22032): Invalid JSON text: "Missing a comma or ']' after an array element." at position 9 in value for column 'zz_TEST_ObsJSON.jsonData'.
Following the JSON.org example http://json.org/example.html does not work:
insert into zz_TEST_ObsJSON (jsonData) values ('{"Test1":{"Val1":"37", "Val2":"25"}},{"Test2":{"Val1":"25", "Val2":"27"}}');ERROR 3140 (22032): Invalid JSON text: "The document root must not follow by other values." at position 36 in value for column 'zz_TEST_ObsJSON.jsonData'.
For giggles, tried putting it all in another { }:
insert into zz_TEST_ObsJSON (jsonData) values ('{{"Test1":{"Val1":"37", "Val2":"25"}},{"Test2":{"Val1":"25", "Val2":"27"}}}');ERROR 3140 (22032): Invalid JSON text: "Missing a name for object member." at position 1 in value for column 'zz_TEST_ObsJSON.jsonData'.
This other method that kind of looks like geoJSON does not either:
insert into zz_TEST_ObsJSON (jsonData) values ('[["Test1":{"Val1":"37", "Val2":"25"}],["Test2":{"Val1":"25", "Val2":"27"}]]');ERROR 3140 (22032): Invalid JSON text: "Missing a comma or ']' after an array element." at position 10 in value for column 'zz_TEST_ObsJSON.jsonData'.
insert into zz_TEST_ObsJSON (jsonData) values ('["Test1":{"Val1":"37", "Val2":"25"}],["Test2":{"Val1":"25", "Val2":"27"}]');ERROR 3140 (22032): Invalid JSON text: "Missing a comma or ']' after an array element." at position 9 in value for column 'zz_TEST_ObsJSON.jsonData'.
Solution
Are you attempting to insert a JSON array with two objects,
If so, then you need to insert the data as an array:
JSON array:
Your
Test1 and Test2?If so, then you need to insert the data as an array:
JSON array:
[{
"Test1": {
"Val1": "37",
"Val2": "25"
}
}, {
"Test2": {
"Val1": "25",
"Val2": "27"
}
}]Your
INSERT statement would look similar to the following (note the wrapping brackets around the JSON objects):INSERT INTO zz_TEST_ObsJSON (jsonData) values ('[{"Test1":{"Val1":"37", "Val2":"25"}},{"Test2":{"Val1":"25", "Val2":"27"}}]');Code Snippets
[{
"Test1": {
"Val1": "37",
"Val2": "25"
}
}, {
"Test2": {
"Val1": "25",
"Val2": "27"
}
}]INSERT INTO zz_TEST_ObsJSON (jsonData) values ('[{"Test1":{"Val1":"37", "Val2":"25"}},{"Test2":{"Val1":"25", "Val2":"27"}}]');Context
StackExchange Database Administrators Q#160731, answer score: 4
Revisions (0)
No revisions yet.