cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

JavaScript Connector handling null values

jharrison
Deputy Chef II
Deputy Chef II

Has anyone had luck handling null values in a JavaScript Connector? I have tried all these below but the error remains "Cannot read properties of null (reading 'regulation')"

regulation = arrayValue.regulation
regulation = arrayValue.regulation || 0
regulation = arrayValue[0].regulation || 0
if(arrayValue.regulation === null){regulation = "null"} else {regulation = arrayValue.regulation}
if(arrayValue.regulation != null){regulation = arrayValue.regulation} else {regulation = "null"}

 

1 ACCEPTED SOLUTION

Found it! The JSON had spacing that was different than the sample file I was using. I realized that all these attempts at finding the correct path to the key were where I needed to focus. So I went back and looked more closely at the parse JSON Output and noticed the spaces in the Output that I had not placed in the sample document of JSON parse, which is why it kept resulting in 'undefined'. Added the spacing and it worked.

Thanks for your help

Jim

View solution in original post

12 REPLIES 12

Hi Gary1,

Well that is annoying!

I tried the index suggestion and it gave me a Regulation undefined error. So at least it's not a null.

I've given up for now. 

Thanks for your assistance

Jim 

gary1
Executive Chef II
Executive Chef II

Very weird! Even if you copy/paste my exact script it still throws the error? If that's the case, there must be something up with the input.

Hi Gary1,

For fun I tried the copy and paste and the error changed to "Cannot read properties of undefined (reading 'Regulation')". 

I think the input is the problem as well. The input JSON has forward slashes in front of all the quotes. I am attempting to get them removed.

I resorted to a different solution, I nested two for loops and got that to work. I feel like the JS may be more efficient over the for loops, which is why I wanted to try to get it to work. 

If I get the JSON cleaned up, I'll try this recipe again and see if the JS solution works. I have it saved. 

Regards,

Jim Harrison

Portland, OR

gary1
Executive Chef II
Executive Chef II

Ah, is the entire input_json escaped with slashes, or only the Status array? Or is it the data in the Status array?

Chances are you probably only need to run one of the above through JSON.parse again. This is all guesswork without seeing the recipe and the payload, but it might be worth a quick try.

Any chance you can share a redacted screenshot or copy/paste of the input_json?

 

const createRecord = (employee) => {
row = {}
// Map top level fields - simple
row.FirstName = employee.FirstName
row.LastName = employee.LastName
row.EmployeeId = employee.EmployeeId
// Map name - nested array

// ====== UPDATES HERE ======
//try this first
row.regulation = JSON.parse(employee.Status)[0].Regulation
//maybe try this is the above doesn't work
//row.regulation = JSON.parse(employee.Status[0]).Regulation


return row
}

exports.main = ({input_json}) => {
const input = JSON.parse(input_json)
const rows = []
input.forEach(employee => {
rows.push(createRecord(employee))
})
return {
rows: rows
}
}

 

 

 

Found it! The JSON had spacing that was different than the sample file I was using. I realized that all these attempts at finding the correct path to the key were where I needed to focus. So I went back and looked more closely at the parse JSON Output and noticed the spaces in the Output that I had not placed in the sample document of JSON parse, which is why it kept resulting in 'undefined'. Added the spacing and it worked.

Thanks for your help

Jim