Does anyone Trim values returned in JSON from an API?

I don’t fully trust data I’m receiving from an outside source via their API. So I’m tossing ‘Trim’ at all the values before commiting them to the database.

Question: Does anyone worry about trimming this sort of data? And if so, is there a shortcut for what I’m doing with 2 steps? (below)

found_enrollment_specialty = enrollment(0).Value("Enrollment Specialty")
found_enrollment_specialty = found_enrollment_specialty.Trim

There’s no hard and fast rule. If it makes sense for your situation, do it.

You could do:

found_enrollment_specialty = enrollment(0).Value("Enrollment Specialty").StringValue.Trim
1 Like

My personal rule-of-thumb is that if the value is coming from a user entered field - then I always perform a Trim (and/or other necessary validation logic), before using the value or saving it to a database or other storage method.

So that in theory, any stored database values should already be clean. But if you’re not in control of what was saved, it certainly doesn’t hurt to err on the side of caution. Unless of course there are so many values you risk a performance hit.

Anyway, to answer your second question. If you know that Variant Value(“Enrolment Specialty”) will always be a string, you could do a one liner like:

found_enrollment_specialty = enrollment(0).Value("Enrollment Specialty").StringValue.Trim()

Or

found_enrollment_specialty = CType(enrollment(0).Value("Enrollment Specialty"), String).Trim()
1 Like

@Scott_C I think we posted less than 10 seconds apart. Great minds think alike!

1 Like

You got it :+1:

But of course I’m more of a wind bag, which is why I came in second place :wink:

1 Like

I appreciate the race to contribute - in my book you’re both winners! :wink:

3 Likes

What are you trimming? Blank spaces or are you trying to get rid of unprintable characters? Which seems to me like it would require more work.

Trimming any leading or trailing space characters.