-1

I'm currently having a JSON file which needs to be converted into a text file with fixed length for each field.

For example, for a JSON

{  
    "employee": {  
        "name":       "ABC",   [length = 10]
        "salary":      1000   [length = 5]
    }  
}  

The output text file should be:

ABC-------1000-

Since I'm new to this, is there any Java library I can use to assign fixed width for each field and create a flat-file for it?

swathi
  • 11
  • 2

1 Answers1

0

You can use this http://jeyben.github.io/fixedformat4j/ to create the string. once you have the String of your needs, then you can write it to a file.

You need to specify which fields are how long and where they begin and what characters should be used for filling. You can even define the alignment of the fields.

Noixes
  • 1,158
  • 12
  • 25
  • Hey, thanks! But does fixedformat4j allow us to pass from JSON to a string? Because all the examples I've seen only use it to parse a string and create a text out of it. – swathi Jul 07 '21 at 12:18
  • With fixedformat4j it is possible to parse an java object to a string and vice versa. That means, you first have to parse you json to an java object. If you have this object you can then use the definitions of fixedformat4j to generate a string out of the object. The resulting string should then always have the same length for all same types of java objects – Noixes Jul 08 '21 at 12:45