Write UUID to JSON file

Updated: Apr 12, 2024

This example will show you how to write UUID field types to JSON files. Similarly, UUID field types can be written to any file with the appropriate DataWriter.

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;
import java.util.UUID;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.core.RecordList;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.json.SimpleJsonWriter;
import com.northconcepts.datapipeline.memory.MemoryReader;

public class WriteUuidToJson {

    public static void main(String[] args) {
        Record record1 = new Record();
        record1.setField("contact_id", UUID.fromString("54e2037c-13c5-42fd-af89-97facd8bfdb8"));
        record1.setField("first_name", "John");
        record1.setField("last_name", "Doe");
 
        Record record2 = new Record();
        record2.setField("contact_id", UUID.fromString("03199f44-943e-49c3-9f56-b876eaeabba5"));
        record2.setField("first_name", "Jane");
        record2.setField("last_name", "Doe");
                
        DataReader reader = new MemoryReader(new RecordList(record1, record2));
         
        DataWriter writer = new SimpleJsonWriter(new File("example/data/output/write-uuid-to-json.json"))
                .setPretty(true);
         
        Job.run(reader, writer);
    }
}

Code Walkthrough

  1. Create two records with sample data and add them to RecordList which will be written to a JSON file.
  2. Create MemoryReader to read these records.
  3. Create SimpleJsonWriter to write the above records to a JSON file.
  4. Records are read from MemoryReader and written to the JSON file via Job.run() method.

Output

The output will be written to the JSON file.

[
  {
    "contact_id":"54e2037c-13c5-42fd-af89-97facd8bfdb8"
    ,"first_name":"John"
    ,"last_name":"Doe"
  }
  ,{
    "contact_id":"03199f44-943e-49c3-9f56-b876eaeabba5"
    ,"first_name":"Jane"
    ,"last_name":"Doe"
  }
]
Mobile Analytics