Using Mockaroo + Go To Create Fake X12 834 Files

Fri, Mar 11, 2022 3-minute read

In the past I had a Mirth channel that would convert X12 834 files to ADTs and CSVs that I needed run a lot of fake/test data through. I use Mockaroo a lot to generate various types of fake data. I haven’t found an easy way to generate X12 directly out of Mockaroo, so decided to spit out JSON with all the segments and fields that I need and write a script to convert that to X12.

I’ve been interested in playing around with Go for a while and used this as an excuse.

Everything described below can be found in this repo:

Mockaroo Schema

If you don’t already have a Mockaroo account, sign up for one. There is a free tier that (as of this writing) allows for:

  • 1,000 rows per file
  • 200 API requests per day

Which is more than enought to generate several tests X12 files daily.

In Mockaroo, you can create the necessary schema by doing a backup restore using the X12-834-JSON.schema.json file in the git repo.

Click on the Schemas link at the top of page and then the Restore From Backup… button:

You’ll then get a page where you can choose the file to restore from:

Once this process completes you will have a Schema entry called X12-834-JSON:

Click on that schema and you’ll see the complete layout:

Get The JSON

Once you have the Mockaroo Schema set, you can start grabbing JSON files with random data to convert. There are a couple ways.

Download From Website

Scroll to the bottom of the Schema and check a few things.

  • The # of rows should be set to 1
  • array should be unchecked
  • include null values should be checked

If this is all set, click the Download Data button at the bottom of the screen:

This will download a file with a default filename of X12-834-JSON.json.

Download From API

Also at the bottom of the Schema you will see a section that shows an example curl call:

You can make that call as is to save the file, later we’ll use that command and instead pipe it through the Go program.

Running The Go Program

There are two files involved:

  • main.go
  • x12834Structs.go

You can of course build an executable:

go build -o Mockaroo834X12 main.go x12834Structs.go

This would create an executable file named Mockaroo834X12. Or you can just run the program:

go run main.go x12834Structs.go

There are a few commmand line arguments that can be specified. Run the program with –help to see them:

amoody@mcp Mockaroo834X12 % go run main.go x12834Structs.go --help
Usage of /var/folders/sr/nwm7y8lj4dj7ffrvp5htslp40000gn/T/go-build868704596/b001/exe/main:
  -element string
    	Element Delimiter (default "*")
  -input string
    	Input File
  -segment string
    	Segment Delimiter (default "~\r\n")
  -subelement string
    	Sub-Element Delimiter (default "/")

So you can set the Element, Sub-Element, and Segment delimiters to use in the X12 output as well as an input file. The input file being the JSON downloaded from Mockaroo.

If you don’t specify and input file it is assumed the input JSON is coming in on stdin.

Some examples of running the program, assuming our Mockaroo output is in /temp/ and named X12-834-JSON.json…

Specify the input file:

go run main.go x12834Structs.go -input /temp/X12-834-JSON.json > /temp/testfile1.x12

Specify the input and change delimiters:

go run main.go x12834Structs.go -element ^ -segment @ -subelement # -input /temp/X12-834-JSON.json > /temp/testfile1.x12

But you can also pipe JSON to the program, so something like:

cat /temp/X12-834-JSON.json | go run main.go x12834Structs.go

So then finally we can call the Mockaroo API directly for some JSON data to pipe through the program:

curl "" | go run main.go x12834Structs.go

Examples Files