0

I have a function to generate an object which I'm transforming to a JSON later on:

function createSearchCriteria(payload) {
    var output = [];
    if (payload['searchCriteria'] != null) {
        for (var i = 0; i < payload['searchCriteria'].length; i++) {
            var content = payload['searchCriteria'][i];
            output[i] = {};
            if (content['grouping'] != null) {
                output[i]['groupOperator'] = content['grouping'];
                output[i]['searchCriteria'] = [];
                output[i]['searchCriteria'].push(createSearchCriteria(content))
            } else {
                output[i]['name'] = content['name'];
                output[i]['type'] = content['type'];
            }
        }
    }
    return output
}

The input payload for this method is also a JSON value parsed

payload = JSON.parse(request);

The input structure is almost the same as the output, the only difference is the "grouping" attribute, which in the output is called "groupOperator".

I have implemented my function recursive because we can have different levels of search criteria.

Even though the searchCriteria in the input has only one [] each.

input data

Why does each searchCriteria in the result comes with 2 pairs of squared brackets?

{
    "searchCriteria": [
        {
            "groupOperator": "AND",
            "searchCriteria": [
                [
                    {
                        "groupOperator": "OR",
                        "searchCriteria": [
                            [
                                {
                                    "name": "FirstName",
                                    "type": "string"
                                },
                                {
                                    "name": "LastName",
                                    "type": "string"
                                },
                                {
                                    "name": "MiddleName",
                                    "type": "string"
                                },
                                {
                                    "name": "Document",
                                    "type": "string"
                                },
                                {
                                    "name": "DOB",
                                    "type": "date"
                                },
                                {
                                    "name": "CdrId",
                                    "type": "string"
                                }
                            ]
                        ]
                    },
                    {
                        "groupOperator": "AND",
                        "searchCriteria": [
                            [
                                {
                                    "name": "Active",
                                    "type": "bool"
                                },
                                {
                                    "name": "Id",
                                    "type": "int"
                                },
                                {
                                    "name": "CountryId",
                                    "type": "int"
                                }
                            ]
                        ]
                    }
                ]
            ]
        }
    ],
    "groupOperator": "AND"
}

Thanks in advance for your help.

3
  • 1
    JSON is completely irrelevant to this question, which is about restructuring an object. You should find better existing solutions if you aren't looking for specifically for a JSON solution. Commented Jan 24, 2021 at 13:14
  • How would you like to change the output? Commented Jan 24, 2021 at 13:38
  • yeah it's irrelevant but the result needed to be a valid json so that's why i mentioned. @LajosArpad, the response that i had contains two sets of squared brackets in the search criteria, I just needed one Commented Jan 25, 2021 at 10:05

1 Answer 1

3

try

output[i]['searchCriteria'] = createSearchCriteria(content)

instead of

output[i]['searchCriteria'] = [];
output[i]['searchCriteria'].push(createSearchCriteria(content))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! Javascript is not my strong suit so... i dind't see that, it works now!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.