-3

How can I create a javascript function to pull data from JSON based on words? I am using an Task Manager API and want to populate fields based on the title of the task. For Example:

{
 "data": {
    "boards": [
      {
        "owner": {
          "id": 555555555
        },
        "groups": [
          {
            "id": "new_group",
            "title": "Forecasts",
            "items": [
              {
                "id": "355670938",
                "name": "PIPE: Production & Client Management | STAGE: New Support Intake | NAME: TESTY | DATE: 10/27/2019 | TIME: 8:00 am | TIME ZONE: Central Time | DURATION: 60 minutes | TYPE: All Virtual | AUDIENCE VIEW:"
              }
            ]
          }
        ]
      }
    ]
  },
  "account_id": 55555555
}

I need the information extracted from items.name for DATE, TIME, DURATION, and TYPE. How could i set up a function to pull just this information?

klaurtar
  • 213
  • 3
  • 19
  • Possible duplicate of [Safely turning a JSON string into an object](https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – computercarguy Oct 22 '19 at 16:31
  • 1
    This question is *way* too broad for Stack Overflow's Q&A format - this site is not a free code-writing service. We'd expect to see at least an attempt at solving the problem (see: [Minimal, Verifiable example](https://stackoverflow.com/help/minimal-reproducible-example)), with information regarding where you're getting stuck, and why your attempt hasn't met your requirements. – esqew Oct 22 '19 at 16:31

3 Answers3

1

You first need to locate where you want to extract the info from.

Then you just split on pipes, and then on colons.

let targetStr = getTree().data.boards[0].groups[0].items[0].name;
let fields = ['DATE', 'TIME', 'DURATION', 'TYPE'];

console.log(extractData(targetStr, fields));

function extractData(str, fields) {
  return str.split(/\s*\|\s*/).reduce((res, entry) => {
    let dat = entry.split(/\s*:\s*/);
    return fields.indexOf(dat[0]) > -1 ? Object.assign(res, { [dat[0]]: dat[1] }) : res;
  }, {});
}

function getTree() {
  return {
    "data": {
      "boards": [{
        "owner": {
          "id": 555555555
        },
        "groups": [{
          "id": "new_group",
          "title": "Forecasts",
          "items": [{
            "id": "355670938",
            "name": "PIPE: Production & Client Management | STAGE: New Support Intake | NAME: TESTY | DATE: 10/27/2019 | TIME: 8:00 am | TIME ZONE: Central Time | DURATION: 60 minutes | TYPE: All Virtual | AUDIENCE VIEW:"
          }]
        }]
      }]
    },
    "account_id": 55555555
  };
}
.as-console-wrapper {
  top: 0;
  max-height: 100% !important;
}
Mr. Polywhirl
  • 35,562
  • 12
  • 75
  • 123
0

Assuming | isn't used ever in the data, you could do:

items.name.split('|').filter(data => /^(DATE:|TIME:|DURATION:|TYPE:)/.test(data));
Anthony
  • 6,159
  • 2
  • 15
  • 31
0

JSON.parse() takes a JSON string and transforms it into a JavaScript object. From there you can just return the value of the desired key.

Buddy
  • 10,627
  • 5
  • 40
  • 57
Sudeep Shrestha
  • 128
  • 1
  • 13