The import endpoint `<http://localhost:8108/collec...
# contributions
h
The import endpoint
<http://localhost:8108/collections/companies/documents/import?action=create>
returns
Copy code
{
  "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
Copy code
{
  "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:
Copy code
{
  "code": 400,
  "document": {
    "id": "124",
    "company_name": "Stark Industries",
    "num_employees": "5215",
    "country": "USA"
  },
  "error": "Field `num_employees` must be an int32.",
  "success": false
}
k
Import end point returns JSON lines as response, where each JSON line records the result of the import operation for the corresponding import record. (i.e. line 1 of response matches line 1 of request and so on)
h
Yep but the response is not valid JSON
k
I don't follow you.
Copy code
{
  "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.
I checked the validity here: https://jsonformatter.curiousconcept.com/
h
Yep just checked it myself, it's valid json
Strange that the json decoder I'm using fails to parse it
Copy code
FormatException: Unexpected character (at character 27)
{"code":400,"document":"{"id": "124","company_name": "Stark Industries","nu...
@Jason Bosco Could you help me understand this? Tried doing this is JS too:
Copy code
const 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:
Copy code
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 27 of the JSON data
Okay found the mistake the
\
should be escaped as well in the nested object
k
@Harpreet Sangar Double escaping seems to work for me. E.g.
Copy code
var str = '{"code":400,"error":"Field `num_employees` must be an int32.","success":false, "document": "{\\"id\\": 100}"}'

JSON.parse(str)
However, the single backslash version throws that error when parsed:
Copy code
var 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
h
@Kishore Nallan Yes. I believe you'll be updating the server response?
I got this response from the latest docker image.
k
Yes, I will fix the server response. Are you saying that the JS client is broken at the moment because of this issue? i.e. when the import has bad data, the JS client fails with JSON parsing error?
h
nah I was implementing a unit test for import function in dart client and found this error. I'll just update my test case to have response with double escaped quotes and assume that's how the response will be in the future.
k
@Harpreet Sangar Actually, I think the current behaviour of single backslash is correct. Try this on Node:
Copy code
var 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);
The generated file looks like this:
Copy code
$ cat /tmp/obj.json
      {"person":"{\"age\":1}"}%
Likewise on Python:
Copy code
import json
f = open('/tmp/obj.json', 'r')
str = f.read()
obj = json.loads(str)
Contents of
/tmp/obj.json
Copy code
% cat /tmp/obj.json
{"person":"{\"age\":1}"}
👍 1
h
Now I understand why an exception was being raised.
Copy code
import '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:
Copy code
original: {person: {"age": 1}}
encoded object: {"person":"{\"age\": 1}"}
decoded object: {person: {"age": 1}}
The last
print
causes the following exception:
Copy code
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 :3
Same with JavaScript as well
k
Oh wait, scratch that.
h
the single quotes is just syntactic sugar iirc, when the vm deals with the strings it converts them to
"
I think something similar is happening in js
k
I'm not familiar with Dart, but do you understand what's happening or do you need any further help here?
h
Nope. I'll handle it. Thank you for your time 🙂
k
👍