Assume you want to parse a fixed position flat file that contains a field for which the value "space" is a valid content and does not mean that this field is empty.

Have a look at the example flat file below. It contains 4 fields: customer (20), country code (2), country name (30) and order balance. For a better visualization, spaces are highlighted by an underscore sign.

A Flat File with Two Records
Autumn & Co.________CA___Canada_________
Spring Corp._____________USA____________

Space is a valid content for the country code. It reflects the home country USA. Divergent countries are specified via dedicated country codes.

This can be a problem because as per default, the flat file parser assumes that "space" is the fill character. The parser will strip off all trailing spaces before parsing the records to objects. If a field consists of the fill character only (as does country code in record two), the attribute will be returned as NULL.
You will get as a result:

{
  "customer" : "Autumn & Co.",
  "country_code" : "CA",
  "country_name" : "Canada"
}

{
  "customer" : "Spring Corp.",
  "country_name": "USA"
}

Avoiding this is very easy. You simply need to set a default value on the affected attribute.

After you have set a default value of e.g. " ", the new result will be:

{
  "customer" : "Autumn & Co.",
  "country_code" : "CA",
  "country_name" : "Canada"
}

{
  "customer" : "Spring Corp.",
  "country_code" : " ",
  "country_name": "USA"
}

Please note, that the behavior is different, if you additionally use padding, e.g. right(" ") on the affected attribute. Using padding will trim off leading or trailing spaces and result in an empty attribute instead of NULL or default value.

  • No labels