1

I am working on a lambda function that GETs data from one API and POSTs it to another. The data is a list of contacts with properties, e.g. first name, last name, email, etc.

The JSON output contains too many properties that I don't need. See below code example (actual code contains many more properties and nested arrays/objects).

{
  "contacts": [
      {
          "addedAt": 1532803458796,
          "vid": 101
      }
   ],
  "merge-audits": [],
  "properties": {
       "first-name": {
          "value":"hello"
        },
        "last-name": {
          "value":"there"
        },
        "email": {
          "value":"[email protected]"
        }
... 
...
}

How can I loop through each JSON object to create a new, simpler JSON array like the following:

[
  {
    "email": "[email protected]",
    "first_name": "",
    "last_name": "User"
  },
  {
    "email": "[email protected]",
    "first_name": "Example",
    "last_name": "User"
  }
]

Thanks in advance for your help.

2
  • JSON is text. When you say "raw JSON" you seem to be really stressing it is JSON... but is it? Or are we starting out with a JavaScript object? Commented Apr 2, 2019 at 20:52
  • does your json have multiple keys called properties or something? Commented Apr 2, 2019 at 20:54

2 Answers 2

1

try

json.map( x => ({
  email:      x.properties.email.value,
  first_name: x.properties['first-name'].value,
  last_name:  x.properties['last-name'].value,
}));

let json = [
{
  "contacts": [{
    "addedAt": 1532803458796,
    "vid": 101
  }],
  "merge-audits": [],
  "properties": {
    "first-name": {
      "value": "hello"
    },
    "last-name": {
      "value": "there",
    },
    "email": {
      "value": "[email protected]"
    }
  }
},
{
  "contacts": [{
    "addedAt": 1532803458796,
    "vid": 101
  }],
  "merge-audits": [],
  "properties": {
    "first-name": {
      "value": "Tom"
    },
    "last-name": {
      "value": "Smith",
    },
    "email": {
      "value": "[email protected]"
    }
  }
}
]

let r = json.map(x => ({
  email:      x.properties.email.value,
  first_name: x.properties['first-name'].value,
  last_name:  x.properties['last-name'].value,
}));

console.log(r);

Sign up to request clarification or add additional context in comments.

1 Comment

Tested, worked perfectly and very efficient, thank you!
1

You could use a destructuring assignment for the object and short hand properties for the mapping.

var data = [{ contacts: [{ addedAt: 1532803458796, vid: 101 }], "merge-audits": [], properties: { "first-name": { value: "hello" }, "last-name": { value: "there" }, email: { value: "[email protected]" } } }],
    result = data.map(({ properties: {
        'first-name': { value: first_name },
        'last-name': { value: last_name },
         email: { value: email }
    } }) => ({ first_name, last_name, email }));

console.log(result);

2 Comments

Your solution also produces the same results. Thank you for the additional information. Would this also work for multiple objects in the data variable?
it does, because data is an array and each same organized object gets converted.

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.