4

I want to test a feature of apache kafka.So I need generate random JSON string in Java.(My production data is JSON format.)

I google it, and I found the sites:

These are not the best answer.

In ruby, the gem faker can generate fake data.

In Java, what library can generate fake data? what library can generate fake JSON string?

Community
  • 1
  • 1
diguage
  • 387
  • 4
  • 19
  • I had exactly the same requirement, and end up building my own library - https://github.com/mangatmodi/RandomJson However, if you want to stream data to Kafka, you could use the following rather than using a library and writing own code - https://github.com/everwatchsolutions/json-data-generator – Mangat Rai Modi Apr 29 '19 at 22:19

2 Answers2

2

Checkout this open source java library randomizer. Based on YOUR_MODEL_CLASS using annotation random data generation is possible.

Generator<YOUR_MODEL_CLASS> generator = new Generator<>(YOUR_MODEL_CLASS.class);
List<YOUR_MODEL_CLASS> fakeObj = generator.generate(NO_OF_RECORDS);
Gson gson = new Gson();
String jsonString = gson.toJson(fakeObj);

There are many inbuilt annotation available,which we can set upon instance variables defined in YOUR_MODEL_CLASS.Go through guide on library page.

Ronak Poriya
  • 2,369
  • 3
  • 18
  • 20
1

You can use mockneat in order to do that. It's a library specialised in generating all kind of "fake" data. Check out the documentation to see what you can "fake" and how.

There is a wiki page that shows you how you can generate a Random JSON:

MockNeat mockNeat = MockNeat.threadLocal();
Gson gson = new GsonBuilder()
                        .setPrettyPrinting()
                        .create();

String json = mockNeat
                     .reflect(UserProfile.class)
                     .field("name", mockNeat.names().full())
                     .field("userName", mockNeat.users())
                     .field("email", mockNeat.emails())
                     .field("profiles",
                                mockNeat.reflect(Profile.class)
                                        .field("profileId", mockNeat.ints().range(100, 1000))
                                        .field("profileAdded", mockNeat.localDates().toUtilDate())
                                        .list(2))
                     .map(gson::toJson) /* Transforms the UserProfile class into a 'pretty' json. */
                     .val();

System.out.println(json);

And the given result is (of course, the results are different each time):

{
  "name": "Cecila Starbird",
  "userName": "moistben",
  "email": "randiexyst@hotmail.co.uk",
  "profiles": [
    {
      "profileId": 964,
      "profileAdded": "Mar 19, 1973 12:00:00 AM"
    },
    {
      "profileId": 854,
      "profileAdded": "Jun 3, 1978 12:00:00 AM"
    }
  ]
}

Later edit:

The new preferred way of generating json is the following: https://www.mockneat.com/tutorial/#json-and-xml

Disclaimer: I am the author of the library.

Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118