Access Array Inside Of String

I’m trying to access some data returned from JotForm that is inside of a string. Here’s the full response I get:

[
  {
    "responseCode": 200,
    "message": "success",
    "content": {
      "id": "redacted",
      "form_id": "redacted",
      "ip": "redacted",
      "created_at": "2022-07-19 11:23:16",
      "status": "ACTIVE",
      "new": "0",
      "flag": "0",
      "notes": "",
      "updated_at": "2022-07-19 11:23:34",
      "answers": {
        "1": {
          "name": "whatIs",
          "order": "1",
          "sublabels": "{\"prefix\":\"Prefix\",\"first\":\"First Name\",\"middle\":\"Middle Name\",\"last\":\"Last Name\",\"suffix\":\"Suffix\"}",
          "text": "What is your name",
          "type": "control_fullname",
          "answer": {
            "first": "John",
            "last": "Doe"
          },
          "prettyFormat": "John Doe"
        },
        "4": {
          "cfname": "Spreadsheet",
          "name": "typeA",
          "order": "4",
          "selectedField": "5321bc1dec233a2e15000006",
          "static": "No",
          "text": "Friends",
          "type": "control_widget",
          "answer": "[[\"#\",\"First Name\",\"Last Name\",\"Email\",\"Phone\"],[1,\"Donald\",\"Duck\",\"[email protected]\",\"444-555-8888\"],[2,\"Abe\",\"Lincoln\",\"[email protected]\",\"555-777-1000\"],[3,\"Clark\",\"Kent\",\"[email protected]\",\"777-111-8888\"],[4,\"\",\"\",\"\",\"\"],[5,\"\",\"\",\"\",\"\"],[6,\"\",\"\",\"\",\"\"],[7,\"\",\"\",\"\",\"\"],[8,\"\",\"\",\"\",\"\"],[9,\"\",\"\",\"\",\"\"],[10,\"\",\"\",\"\",\"\"],[11,\"\",\"\",\"\",\"\"],[12,\"\",\"\",\"\",\"\"],[13,\"\",\"\",\"\",\"\"],[14,\"\",\"\",\"\",\"\"],[15,\"\",\"\",\"\",\"\"],[16,\"\",\"\",\"\",\"\"],[17,\"\",\"\",\"\",\"\"],[18,\"\",\"\",\"\",\"\"],[19,\"\",\"\",\"\",\"\"],[20,\"\",\"\",\"\",\"\"],[21,\"\",\"\",\"\",\"\"],[22,\"\",\"\",\"\",\"\"],[23,\"\",\"\",\"\",\"\"],[24,\"\",\"\",\"\",\"\"],[25,\"\",\"\",\"\",\"\"],[26,\"\",\"\",\"\",\"\"],[27,\"\",\"\",\"\",\"\"],[28,\"\",\"\",\"\",\"\"],[29,\"\",\"\",\"\",\"\"],[30,\"\",\"\",\"\",\"\"],[31,\"\",\"\",\"\",\"\"],[32,\"\",\"\",\"\",\"\"],[33,\"\",\"\",\"\",\"\"],[34,\"\",\"\",\"\",\"\"],[35,\"\",\"\",\"\",\"\"],[36,\"\",\"\",\"\",\"\"],[37,\"\",\"\",\"\",\"\"],[38,\"\",\"\",\"\",\"\"],[39,\"\",\"\",\"\",\"\"],[40,\"\",\"\",\"\",\"\"],[41,\"\",\"\",\"\",\"\"],[42,\"\",\"\",\"\",\"\"],[43,\"\",\"\",\"\",\"\"],[44,\"\",\"\",\"\",\"\"],[45,\"\",\"\",\"\",\"\"],[46,\"\",\"\",\"\",\"\"],[47,\"\",\"\",\"\",\"\"],[48,\"\",\"\",\"\",\"\"],[49,\"\",\"\",\"\",\"\"],[50,\"\",\"\",\"\",\"\"],[51,\"\",\"\",\"\",\"\"],[52,\"\",\"\",\"\",\"\"],[53,\"\",\"\",\"\",\"\"],[54,\"\",\"\",\"\",\"\"],[55,\"\",\"\",\"\",\"\"],[56,\"\",\"\",\"\",\"\"],[57,\"\",\"\",\"\",\"\"],[58,\"\",\"\",\"\",\"\"],[59,\"\",\"\",\"\",\"\"],[60,\"\",\"\",\"\",\"\"],[61,\"\",\"\",\"\",\"\"],[62,\"\",\"\",\"\",\"\"],[63,\"\",\"\",\"\",\"\"],[64,\"\",\"\",\"\",\"\"],[65,\"\",\"\",\"\",\"\"],[66,\"\",\"\",\"\",\"\"],[67,\"\",\"\",\"\",\"\"],[68,\"\",\"\",\"\",\"\"],[69,\"\",\"\",\"\",\"\"],[70,\"\",\"\",\"\",\"\"],[71,\"\",\"\",\"\",\"\"],[72,\"\",\"\",\"\",\"\"],[73,\"\",\"\",\"\",\"\"],[74,\"\",\"\",\"\",\"\"],[75,\"\",\"\",\"\",\"\"],[76,\"\",\"\",\"\",\"\"],[77,\"\",\"\",\"\",\"\"],[78,\"\",\"\",\"\",\"\"],[79,\"\",\"\",\"\",\"\"],[80,\"\",\"\",\"\",\"\"],[81,\"\",\"\",\"\",\"\"],[82,\"\",\"\",\"\",\"\"],[83,\"\",\"\",\"\",\"\"],[84,\"\",\"\",\"\",\"\"],[85,\"\",\"\",\"\",\"\"],[86,\"\",\"\",\"\",\"\"],[87,\"\",\"\",\"\",\"\"],[88,\"\",\"\",\"\",\"\"],[89,\"\",\"\",\"\",\"\"],[90,\"\",\"\",\"\",\"\"],[91,\"\",\"\",\"\",\"\"],[92,\"\",\"\",\"\",\"\"],[93,\"\",\"\",\"\",\"\"],[94,\"\",\"\",\"\",\"\"],[95,\"\",\"\",\"\",\"\"],[96,\"\",\"\",\"\",\"\"],[97,\"\",\"\",\"\",\"\"],[98,\"\",\"\",\"\",\"\"],[99,\"\",\"\",\"\",\"\"],[100,\"\",\"\",\"\",\"\"]]"
        },
        "5": {
          "name": "whatIs5",
          "order": "2",
          "text": "What is your email?",
          "type": "control_email",
          "answer": "[email protected]"
        },
        "6": {
          "name": "whatIs6",
          "order": "3",
          "sublabels": "{\"country\":\"Country Code\",\"area\":\"Area Code\",\"phone\":\"Phone Number\",\"full\":\"Phone Number\",\"masked\":\"Please enter a valid phone number.\"}",
          "text": "What is your Phone Number?",
          "type": "control_phone",
          "answer": {
            "full": "(555) 555-0000"
          },
          "prettyFormat": "(555) 555-0000"
        }
      }
    },
    "duration": "49.39ms",
    "limit-left": 9975
  }
]

I’m trying to access the values inside of the arrays from the answer key:

{
  "cfname": "Spreadsheet",
  "name": "typeA",
  "order": "4",
  "selectedField": "5321bc1dec233a2e15000006",
  "static": "No",
  "text": "Friends",
  "type": "control_widget",
  "answer": "[[\"#\",\"First Name\",\"Last Name\",\"Email\",\"Phone\"],[1,\"Donald\",\"Duck\",\"[email protected]\",\"444-555-8888\"],[2,\"Abe\",\"Lincoln\",\"[email protected]\",\"555-777-1000\"],[3,\"Clark\",\"Kent\",\"[email protected]\",\"777-111-8888\"],[4,\"\",\"\",\"\",\"\"],[5,\"\",\"\",\"\",\"\"],[6,\"\",\"\",\"\",\"\"],[7,\"\",\"\",\"\",\"\"],[8,\"\",\"\",\"\",\"\"],[9,\"\",\"\",\"\",\"\"],[10,\"\",\"\",\"\",\"\"],[11,\"\",\"\",\"\",\"\"],[12,\"\",\"\",\"\",\"\"],[13,\"\",\"\",\"\",\"\"],[14,\"\",\"\",\"\",\"\"],[15,\"\",\"\",\"\",\"\"],[16,\"\",\"\",\"\",\"\"],[17,\"\",\"\",\"\",\"\"],[18,\"\",\"\",\"\",\"\"],[19,\"\",\"\",\"\",\"\"],[20,\"\",\"\",\"\",\"\"],[21,\"\",\"\",\"\",\"\"],[22,\"\",\"\",\"\",\"\"],[23,\"\",\"\",\"\",\"\"],[24,\"\",\"\",\"\",\"\"],[25,\"\",\"\",\"\",\"\"],[26,\"\",\"\",\"\",\"\"],[27,\"\",\"\",\"\",\"\"],[28,\"\",\"\",\"\",\"\"],[29,\"\",\"\",\"\",\"\"],[30,\"\",\"\",\"\",\"\"],[31,\"\",\"\",\"\",\"\"],[32,\"\",\"\",\"\",\"\"],[33,\"\",\"\",\"\",\"\"],[34,\"\",\"\",\"\",\"\"],[35,\"\",\"\",\"\",\"\"],[36,\"\",\"\",\"\",\"\"],[37,\"\",\"\",\"\",\"\"],[38,\"\",\"\",\"\",\"\"],[39,\"\",\"\",\"\",\"\"],[40,\"\",\"\",\"\",\"\"],[41,\"\",\"\",\"\",\"\"],[42,\"\",\"\",\"\",\"\"],[43,\"\",\"\",\"\",\"\"],[44,\"\",\"\",\"\",\"\"],[45,\"\",\"\",\"\",\"\"],[46,\"\",\"\",\"\",\"\"],[47,\"\",\"\",\"\",\"\"],[48,\"\",\"\",\"\",\"\"],[49,\"\",\"\",\"\",\"\"],[50,\"\",\"\",\"\",\"\"],[51,\"\",\"\",\"\",\"\"],[52,\"\",\"\",\"\",\"\"],[53,\"\",\"\",\"\",\"\"],[54,\"\",\"\",\"\",\"\"],[55,\"\",\"\",\"\",\"\"],[56,\"\",\"\",\"\",\"\"],[57,\"\",\"\",\"\",\"\"],[58,\"\",\"\",\"\",\"\"],[59,\"\",\"\",\"\",\"\"],[60,\"\",\"\",\"\",\"\"],[61,\"\",\"\",\"\",\"\"],[62,\"\",\"\",\"\",\"\"],[63,\"\",\"\",\"\",\"\"],[64,\"\",\"\",\"\",\"\"],[65,\"\",\"\",\"\",\"\"],[66,\"\",\"\",\"\",\"\"],[67,\"\",\"\",\"\",\"\"],[68,\"\",\"\",\"\",\"\"],[69,\"\",\"\",\"\",\"\"],[70,\"\",\"\",\"\",\"\"],[71,\"\",\"\",\"\",\"\"],[72,\"\",\"\",\"\",\"\"],[73,\"\",\"\",\"\",\"\"],[74,\"\",\"\",\"\",\"\"],[75,\"\",\"\",\"\",\"\"],[76,\"\",\"\",\"\",\"\"],[77,\"\",\"\",\"\",\"\"],[78,\"\",\"\",\"\",\"\"],[79,\"\",\"\",\"\",\"\"],[80,\"\",\"\",\"\",\"\"],[81,\"\",\"\",\"\",\"\"],[82,\"\",\"\",\"\",\"\"],[83,\"\",\"\",\"\",\"\"],[84,\"\",\"\",\"\",\"\"],[85,\"\",\"\",\"\",\"\"],[86,\"\",\"\",\"\",\"\"],[87,\"\",\"\",\"\",\"\"],[88,\"\",\"\",\"\",\"\"],[89,\"\",\"\",\"\",\"\"],[90,\"\",\"\",\"\",\"\"],[91,\"\",\"\",\"\",\"\"],[92,\"\",\"\",\"\",\"\"],[93,\"\",\"\",\"\",\"\"],[94,\"\",\"\",\"\",\"\"],[95,\"\",\"\",\"\",\"\"],[96,\"\",\"\",\"\",\"\"],[97,\"\",\"\",\"\",\"\"],[98,\"\",\"\",\"\",\"\"],[99,\"\",\"\",\"\",\"\"],[100,\"\",\"\",\"\",\"\"]]"
}

How do I access the arrays that are inside of this string?

What youre trying to do is turn a string into an array and the closest thing I can think of is by using the String.split function which will turn your string into an array of substrings but it might not give you the result you were hoping for. Another thing that immediately came to mind was the built in JSON.parse function which turns a string into a JSON object but your string isnt structured properly

So would it be possible for you to modify this data so that it gets returned as an object instead of a string

EDIT: oops JSON.parse did end up working, i just copied the data wrong :smiley:

Hi @jhambach!

It looks like the answer contains a JSON string. You’d need to parse the value using JSON.parse() to access it as an array.

Here is a rough example that converts the answers array for answer 4 to an actual array:

Taking it a step further, you could even normalize the array of arrays that gets parsed into an array of objects that use the first array element as the header like this:

const answers = JSON.parse(item.content.answers["4"].answer);

const normalizedAnswers = [];

if (Array.isArray(answers) && answers.length) {
  const headers = answers.shift();
  for (const answer of answers) {
    const normalized = {};
    for (let i = 0; i<headers.length; i++) {
      normalized[headers[i]] = answer[i];
    }
    normalizedAnswers.push(normalized);
  }

  item.content.answers["4"].answer = normalizedAnswers;
}

return item;

This allows you to convert the parsed array that looks like this:

  [
    "#",
    "First Name",
    "Last Name",
    "Email",
    "Phone"
  ],
  [
    1,
    "Donald",
    "Duck",
    "[email protected]",
    "444-555-8888"
  ],
  [
    2,
    "Abe",
    "Lincoln",
    "[email protected]",
    "555-777-1000"
  ],
  ...
]

to something like this:

[
  {
    "#": 1,
    "First Name": "Donald",
    "Last Name": "Duck",
    "Email": "[email protected]",
    "Phone": "444-555-8888"
  },
  {
    "#": 2,
    "First Name": "Abe",
    "Last Name": "Lincoln",
    "Email": "[email protected]",
    "Phone": "555-777-1000"
  },
  ....
]
4 Likes

Wow I could hug you right now, thank you so much! Let me know if I can send you a case or two of beer or a beverage of you choice!

1 Like

You’re welcome, happy to help!

1 Like