Harpreet Sangar
06/07/2021, 1:58 PM<http://localhost:8108/collections/companies/documents/import?action=create>
returns
{
"code": 400,
"document": "{\"id\": \"124\",\"company_name\": \"Stark Industries\",\"num_employees\": \"5215\",\"country\": \"USA\"}",
"error": "Field `num_employees` must be an int32.",
"success": false
}
for the request body
{
"id": "124",
"company_name": "Stark Industries",
"num_employees": "5215",
"country": "USA"
}
which is fine except it is not valid JSON. I'm just implementing import function and the code is supposed to throw ImportError but if fails before that due to parsing failure. It should return something like this:
{
"code": 400,
"document": {
"id": "124",
"company_name": "Stark Industries",
"num_employees": "5215",
"country": "USA"
},
"error": "Field `num_employees` must be an int32.",
"success": false
}
Kishore Nallan
06/07/2021, 2:01 PMHarpreet Sangar
06/07/2021, 2:02 PMKishore Nallan
06/07/2021, 2:03 PM{
"code": 400,
"document": "{\"id\": \"124\",\"company_name\": \"Stark Industries\",\"num_employees\": \"5215\",\"country\": \"USA\"}",
"error": "Field `num_employees` must be an int32.",
"success": false
}
is valid JSON.Kishore Nallan
06/07/2021, 2:04 PMHarpreet Sangar
06/07/2021, 2:09 PMHarpreet Sangar
06/07/2021, 2:09 PMFormatException: Unexpected character (at character 27)
{"code":400,"document":"{"id": "124","company_name": "Stark Industries","nu...
Harpreet Sangar
06/07/2021, 4:33 PMconst resultsInJSONLFormat = '{"code":400,"document":"{\"id\": \"124\",\"company_name\": \"Stark Industries\",\"num_employees\": \"5215\",\"country\": \"USA\"}","error":"Field `num_employees` must be an int32.","success":false}'
const resultsInJSONFormat = resultsInJSONLFormat.split('\n').map(r => JSON.parse((r)))
console.log(resultsInJSONFormat)
which results in a similar error:
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 27 of the JSON data
Harpreet Sangar
06/07/2021, 4:36 PM\
should be escaped as well in the nested objectKishore Nallan
06/08/2021, 3:58 AMvar str = '{"code":400,"error":"Field `num_employees` must be an int32.","success":false, "document": "{\\"id\\": 100}"}'
JSON.parse(str)
Kishore Nallan
06/08/2021, 3:58 AMvar str = '{"code":400,"error":"Field `num_employees` must be an int32.","success":false, "document": "{\"id\": 100}"}'
JSON.parse(str)
Uncaught SyntaxError: Unexpected token i in JSON at position 94
Harpreet Sangar
06/08/2021, 4:47 AMHarpreet Sangar
06/08/2021, 4:49 AMKishore Nallan
06/08/2021, 6:11 AMHarpreet Sangar
06/08/2021, 6:26 AMKishore Nallan
06/08/2021, 10:16 AMvar fs = require('fs');
var file_path = '/tmp/obj.json';
var str = '{"age":1}';
var obj = {};
obj['person'] = str;
fs.writeFileSync(file_path, JSON.stringify(obj));
var objstr = fs.readFileSync(file_path, {encoding:'utf8', flag:'r'});
var obj2 = JSON.parse(objstr)
console.log(obj2);
Kishore Nallan
06/08/2021, 10:17 AM$ cat /tmp/obj.json
{"person":"{\"age\":1}"}%
Kishore Nallan
06/08/2021, 10:20 AMimport json
f = open('/tmp/obj.json', 'r')
str = f.read()
obj = json.loads(str)
Contents of /tmp/obj.json
% cat /tmp/obj.json
{"person":"{\"age\":1}"}
Harpreet Sangar
06/08/2021, 12:31 PMimport 'dart:convert';
void main() {
final str = '{"age": 1}',
obj = {'person': str},
encoded = json.encode(obj);
print('original: $obj');
print('encoded object: $encoded');
print('decoded object: ${json.decode(encoded)}');
final myEncoded = '{"person":"{\"age\": 1}"}';
print(json.decode(myEncoded));
}
The first three print
work fine and output:
original: {person: {"age": 1}}
encoded object: {"person":"{\"age\": 1}"}
decoded object: {person: {"age": 1}}
The last print
causes the following exception:
Unhandled exception:
FormatException: Unexpected character (at character 14)
{"person":"{"age": 1}"}
^
Seems like I cannot pass in the encoded string manually. Talk about being fair :3Harpreet Sangar
06/08/2021, 12:31 PMKishore Nallan
06/08/2021, 12:33 PMHarpreet Sangar
06/08/2021, 12:33 PM"
Harpreet Sangar
06/08/2021, 12:34 PMKishore Nallan
06/08/2021, 12:34 PMHarpreet Sangar
06/08/2021, 12:36 PMKishore Nallan
06/08/2021, 12:36 PM