Within my code snippet below, I am able to retrieve a JSON Response:
respArray = []
respArray = respBody.classifiers
respBody = respArray
if (respBody.length > 0) {
respBody = applyPagination(respBody, reqParams.filter, options)
}
The JSON response looks like this:
[
{
"classifier_id": "improper",
"name": "improper",
"owner": "",
"status": "failed",
"core_ml_enabled": true,
"explanation": "",
"created": "2019-07-04T14:28:37.402Z",
"classes": [
{
"class": "impropertest"
}
],
"updated": "2019-07-04T14:28:37.402Z"
},
{
"classifier_id": "improper",
"name": "improper",
"owner": "",
"status": "failed",
"core_ml_enabled": true,
"explanation": "",
"created": "2019-07-05T08:31:55.463Z",
"classes": [
{
"class": "impropertest"
}
],
"updated": "2019-07-05T08:31:55.463Z"
}]
In addition to this dynamic JSON response, I have a predefined static JSON that I intend to append:
Sample Static JSON:
"defaultClassifiers":{
"classifiers": [
{
"classifier_id": "general",
"name": "general",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "general"
}
],
"core_ml_enabled": false
},
{
"classifier_id": "explicit",
"name": "explicit",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "explicit"
}
],
"core_ml_enabled": false
},
{
"classifier_id": "food",
"name": "food",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "food"
}
],
"core_ml_enabled": false
}
]}
My attempt to merge these two JSONs is as follows:
respArray = []
var defaultClassifiers = constants.defaultClassifiers.classifiers
respArray.push(defaultClassifiers,respBody.classifiers)
respBody = respArray
if (respBody.length > 0) {
respBody = applyPagination(respBody, reqParams.filter, options)
}
The current output obtained is:
[
{
"0": {
"classifier_id": "general",
"name": "general",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "general"
}
],
"core_ml_enabled": false
},
"1": {
"classifier_id": "explicit",
"name": "explicit",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "explicit"
}
],
"core_ml_enabled": false
},
"2": {
"classifier_id": "food",
"name": "food",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "food"
}
],
"core_ml_enabled": false
}
},
{
"0": {
"classifier_id": "link_877889562",
"name": "link",
"status": "ready",
"owner": "",
"created": "2019-07-05T04:10:06.457Z",
"updated": "2019-07-05T04:10:06.457Z",
"classes": [
{
"class": "classcreatezip1"
}
],
"core_ml_enabled": true
},
"1": {
"classifier_id": "classifer1807",
"name": "classifer1807",
"status": "ready",
"owner": "",
"created": "2019-07-25T10:43:00.012Z",
"updated": "2019-07-25T10:43:00.012Z",
"classes": [
{
"class": "class18"
}
],
"core_ml_enabled": true
}]
Expected output was:
[
{
"classifier_id": "general",
"name": "general",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "general"
}
],
"core_ml_enabled": false
},
{
"classifier_id": "explicit",
"name": "explicit",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "explicit"
}
],
"core_ml_enabled": false
},
{
"classifier_id": "food",
"name": "food",
"status": "ready",
"owner": "",
"created": "",
"updated": "",
"classes": [
{
"class": "food"
}
],
"core_ml_enabled": false
},
{
"classifier_id": "improper",
"name": "improper",
"owner": "",
"status": "failed",
"core_ml_enabled": true,
"explanation": "",
"created": "2019-07-04T14:28:37.402Z",
"classes": [
{
"class": "impropertest"
}
],
"updated": "2019-07-04T14:28:37.402Z"
},
{
"classifier_id": "improper",
"name": "improper",
"owner": "",
"status": "failed",
"core_ml_enabled": true,
"explanation": "",
"created": "2019-07-05T08:31:55.463Z",
"classes": [
{
"class": "impropertest"
}
],
"updated": "2019-07-05T08:31:55.463Z"
}]
It seems like the merging process went wrong causing the array to display with numerical keys. I'm unsure of my mistake here.