0

I have a requirement where I need to partition incoming data to look like the following example:

//required payload

{
"campaign_id": "",
"recipient": {
    "first_name": "",
    "last_name": "",
    "company": "",
    "email": "",
    "address_1": "",
    "city": "",
    "state": "",
    "postal_code": "",
    "identifier": ""
    }
}

However, my issue is that the payload currently being posted to my endpoint looks like this:

//entire payload

"body": {
"info": [
{
"campaign_id": "",
"recipient": {
    "first_name": "",
    "last_name": "",
    "company": "",
    "email": "",
    "address_1": "",
    "city": "",
    "state": "",
    "postal_code": "",
    "identifier": ""
    }
}
],
"otherArray": [],
"otherString": "",
"otherString2": ""
}
} 

What I want to do is extract the required data in my first example from the second example and then store it in a variable. Something like the below:

var requiredPayload = {"campaign_id": "", "recipient": {}};

What can I do to put campaign_id + recipient object in a single variable?

2 Answers 2

2

try this:

    var data={"body": {
        "info": [
        {
        "campaign_id": "",
        "recipient": {
            "first_name": "",
            "last_name": "",
            "company": "",
            "email": "",
            "address_1": "",
            "city": "",
            "state": "",
            "postal_code": "",
            "identifier": ""
        }
        }
        ],
        "otherArray": [],
        "otherString": "",
        "otherString2": ""
        }
        };
    console.log(data.body.info[0]); //data is variable name
var info_data=data.body.info[0];

if info have multiple campaign_id then try this

$.each(data.body.info, function (indexInArray, valueOfElement) { 
    console.log(data.body.info[indexInArray]);
});
Sign up to request clarification or add additional context in comments.

Comments

1

We should be able to assign the first element of the info array to the requiredPayload variable, like so:

 
   const payload = {
    "body": {
        "info": [
            {
                "campaign_id": "",
                "recipient": {
                    "first_name": "",
                    "last_name": "",
                    "company": "",
                    "email": "",
                    "address_1": "",
                    "city": "",
                    "state": "",
                    "postal_code": "",
                    "identifier": ""
                }
            }
        ],
        "otherArray": [],
        "otherString": "",
        "otherString2": ""
    }
};

var requiredPayload = payload.body.info[0];
console.log("requiredPayload:", requiredPayload);

Comments

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.