1

I have encountered a JSON Structure I have never seen before. This is using Square Bracket ([]) but has spaces in the name. How would I navigate such a structure?

"profile": [{
  "[Profile ID]": 1001398965,
  "[Name | Prefix]": "Ms.",
  "[Name | First]": "Lori",
  "[Name | Middle]": "",
  "[Name | Last]": "Smith",
  "[Name | Suffix]": "",
  "[Contact Name]": "Lori Smith"
},
{  "[Profile ID]": 1001398965,
  "[Name | Prefix]": "Ms.",
  "[Name | First]": "Jeanine",
  "[Name | Middle]": "",
  "[Name | Last]": "Samson",
  "[Name | Suffix]": "",
  "[Contact Name]": "Jeanine Samson"
}]

I have tried

profile[0]['Name | First']  //result undefined

profile[0][Name | First]   //result Name is not defined

Any help is appreciated.

1
  • profile[0][Name | First]: your program is looking for "Name" and "First" variables. Commented Feb 20, 2021 at 19:42

2 Answers 2

1

Brackets are part of the key. You have to use:

profile[0]['[Name | First]']
Sign up to request clarification or add additional context in comments.

Comments

1
j['profile'][0]['[Name | Prefix]']

See this snippet:

var j = {
  "profile": [{
    "[Profile ID]": 1001398965,
    "[Name | Prefix]": "Ms.",
    "[Name | First]": "Lori",
    "[Name | Middle]": "",
    "[Name | Last]": "Smith",
    "[Name | Suffix]": "",
    "[Contact Name]": "Lori Smith"
  },
  {  "[Profile ID]": 1001398965,
    "[Name | Prefix]": "Ms.",
    "[Name | First]": "Jeanine",
    "[Name | Middle]": "",
    "[Name | Last]": "Samson",
    "[Name | Suffix]": "",
    "[Contact Name]": "Jeanine Samson"
  }]
};

console.log(j['profile'][0]['[Name | Prefix]']);

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.