JanC
January 9, 2020, 1:03pm
1
Im trying to import a json object, but sadly I am always getting the error "message":"Malformed JSON body, rows and uniqueKeys are required","code":422,"data":[]
.
My json object and the path are both confirmed to be correct since I tested with Postman beforehand.
My call in nodeJS with the request package looks like this
request.post({url: baseUrl + '/importjson/' + dataSetId, json: true, body: jsonFile, auth: {bearer: token}})
.then(response => {
resolve(response);
})
.catch(errOnCall => reject(errOnCall));
I think the issue is within how I pass the jsonFile
, but I could not find the right way.
JanC
January 10, 2020, 1:39pm
2
Since I wanted to read the json from a json file. I had to read the file, turn it into a string and then parse it into JSON.
callImportJSON = function (dataSetId, jsonFile, token) {
return new Promise((resolve, reject) => {
validateParameters.validateFileExtension('.json', jsonFile)
.then(() => {
prepareImportJSON(jsonFile)
.then(jsonObject => {
request.post({
url: baseUrl + '/importjson/' + dataSetId,
json: jsonObject,
auth: {bearer: token}
})
.then(response => {
logging.passToLog('Imported to data set ' + dataSetId, loggingFile);
resolve(response);
})
.catch(errOnCall => reject(errOnCall));
});
})
.catch(errOnExtensionsValidation => reject(errOnExtensionsValidation));
});
};
prepareImportJSON = function (file) {
return new Promise((resolve) => {
const readFile = fs.readFileSync(file);
const readFileToJSON = JSON.parse(readFile.toString());
resolve(readFileToJSON);
})
};