0

I have the following JSON I am trying to parse the first object key/val Member ID (U1) but I am getting undefined. Any solution around this? I am retrieving the data from an external 3rd party API.

var data = { data:
               [ { 'Member ID (U1)': 'monkey!',
                   MID: '123456',
                   'Advertiser Name': 'SampleAdvertiser',
                   'Order ID': 'ORDER12345',
                   'Transaction Date': '6/16/20',
                   'Transaction Time': '11:13:14',
                   SKU: 'SKU12345',
                   Sales: '100',
                   '# of Items': '1',
                   'Total Commission': '12.8',
                   'Process Date': '6/17/20',
                   'Process Time': '11:20:36' }]
            }

data.data.forEach(transaction => {
    var member_id = transaction['Member ID (U1)']
    console.log(member_id) //prints undefined, expected monkey!
})

edit - turns out there is some weird special character in front of the "M". ended up copying that character in the for loop and now it works. Thank you SO!

3
  • That is not JSON. Could you check the usage description of the json tag? Commented Jun 17, 2020 at 19:41
  • 1
    You had symbol in that 'Member ID (U1)'. Without it it gives the expected result.(Like in that code snippet) Commented Jun 17, 2020 at 19:42
  • I tried to turn the script into a code snippet, but SO removed the special character once I saved the snippet. So I reverted my update of your question. The special character sits just before the initial "M". Commented Jun 17, 2020 at 19:45

3 Answers 3

3

The key Member ID (U1) contains a ZERO WIDTH NO-BREAK SPACE' (U+FEFF) so when you try to access it without that invisible character then it is undefined. You can access the key like this:

var member_id = transaction["\uFEFFMember ID (U1)"]

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

Comments

1

There are special / hidden characters in 'Member ID (U1)'. Not sure what the characters are. How did you get the data?

I copied the key value from the var data declaration, pasted in the foreach code and it worked. see in https://jsfiddle.net/mk4q3x9L/

var data = { data:
               [ { 'Member ID (U1)': 'monkey!',
                   MID: '123456',
                   'Advertiser Name': 'SampleAdvertiser',
                   'Order ID': 'ORDER12345',
                   'Transaction Date': '6/16/20',
                   'Transaction Time': '11:13:14',
                   SKU: 'SKU12345',
                   Sales: '100',
                   '# of Items': '1',
                   'Total Commission': '12.8',
                   'Process Date': '6/17/20',
                   'Process Time': '11:20:36' }]
            }


data.data.forEach(transaction => {
   var member_id = transaction['Member ID (U1)']
    console.log(member_id) //prints undefined, expected monkey!
})

Edit - As @apena pointed out the offending character is a 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF)

3 Comments

Makes sense - thats so odd! This is coming from a 3rd party via a csv which I have converted into a JSON.
not sure how or why that special char is there. but ended up copying that char in the for loop. Thank you!
@gk103 - glad it worked. As other answers pointed out it is a zero width non-break space.
1
var data = { data:
           [ { 'Member ID (U1)': 'monkey!',
               MID: '123456',
               'Advertiser Name': 'SampleAdvertiser',
               'Order ID': 'ORDER12345',
               'Transaction Date': '6/16/20',
               'Transaction Time': '11:13:14',
               SKU: 'SKU12345',
               Sales: '100',
               '# of Items': '1',
               'Total Commission': '12.8',
               'Process Date': '6/17/20',
               'Process Time': '11:20:36' }]
        }

data.data.forEach(transaction => {
    var member_id = transaction['Member ID (U1)'];
    console.log(member_id); //prints monkey!
})

worked as expected! https://codepen.io/stewardtz/pen/rNxeREN

You have some extra char inside single quotes before words "Member ID (U1)". Please check my codepen example (open Console tab on the bottom).

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.