Marc
Marc

Reputation: 3

Javascript return certain string from body

i am totally new to Javascript so after some help

if (BODY.length && BODY[0]["id"]) {
    result = {
        "EmpID": BODY,
        "Status": "User found",
        "Code": 200
    };
} else {
    result = {
    "Status": "User not found",
    "Code": 404
    };
}

I have the above script which pulls data from a response body and i get a return of

{
    "EmpID": [
        {
            "id": "1EF7C992-CBC1-45AF-83CC-304044E8284B"
        }
    ],
    "Status": "User found",
    "Code": 200
}

the id changes depending on the user but i just need the EmpID to be

"EmpID": 1EF7C992-CBC1-45AF-83CC-304044E8284B or whatever the code is returned without the { } and "id":

e.g

{
    "EmpID": 1EF7C992-CBC1-45AF-83CC-304044E8284B
    "Status": "User found",
    "Code": 200
}

Can anyone help?

Thank you

Upvotes: 0

Views: 72

Answers (2)

Utsav Patel
Utsav Patel

Reputation: 2889

If this is the response you get.

{
    "EmpID": [
        {
            "id": "1EF7C992-CBC1-45AF-83CC-304044E8284B"
        }
    ],
    "Status": "User found",
    "Code": 200
}

then you can return

{
"EmpID": response["EmpID"][0].id,
"Status": "User found",
 "Code": 200
}

Upvotes: 0

Tom
Tom

Reputation: 2944

You need to extract the ID from the correct location in your return statement just like you do when you check it exists in your if statement e.g.

result = {
    "EmpID": BODY[0]["id"],
    "Status": "User found",
    "Code": 200
};

Upvotes: 2

Related Questions