Refactor: reduce similar code

By making it into function.
Merge branch 'develop' of https://github.com/Opetushallitus/koodisto into develop