0

I am testing REST APIs. Each API consumers the different type of JSON payload. I don't want to fill all input manually. So, I want to generate JSON dynamically (e.g. read values from a text file and fill in JSON structure) and then pass the generated JSON as Request Body in API.

What is the best way to do so? Any recommendations for tools or a plugin?

P.S. The JSON structure in Nested and very complex.

  • Does this answer your question? [How to convert hashmap to JSON object in Java](https://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java) – hiropon Apr 05 '20 at 06:55
  • *"read values from a text file and fill in JSON structure"* Make the the text file format JSON, then send the content unmodified. – Andreas Apr 05 '20 at 06:56
  • What REST API are you using? Most Java REST APIs will automatically convert a `Map` to a JSON object, and vice-versa. Same for a `List` or `Object[]` to a JSON array. – Andreas Apr 05 '20 at 06:58

2 Answers2

1

This is the same I was having some weeks ago.

What I did, might be helpful to you too:

I used private Map<String, Object> data;

in my DTO where I wanted to have Dynamic JSON.

like if my JSON is:

{
"key":{
"key1":["1","2","3"]
},
"key2":{
}
}

then this JSON will be saved as a Map which you can use to parse your JSON data.

and for parsing try using org.json

For example:

JSONObject jsonObject = new JSONObject(mapFromDTO);

And now you have full access to JSON, which was your core issue.

Deepak Singh
  • 86
  • 4
  • 27
  • @saurav-dudeja if it works for you please accept the answer so the future coders can directly refer to this solution. – Deepak Singh Apr 05 '20 at 07:53
0

The answer lies in defining the contract. Decide first on what are the maximum possible values in your JSON.

Example, for start you can decide on following contact:

{
    "id": 1,
    "name": "Username",
    "age": 30,
    "phone": 900000000
}

Once contract is finalised, design a POJO. This POJO can be very complex data structure which can have fields or may be list of fields (objects). This is totally dependent on your business.

You can then write a java service which can generate this POJO based on some complex business logic.

Once this POJO is populated use 3rd party library like jackson to convert it to JSON.

Further reading: https://www.tutorialspoint.com/how-to-convert-java-object-to-json-using-jackson-library

Rupesh
  • 2,627
  • 1
  • 28
  • 42
  • that's not the right way to do that, because if any new key arrives in the future, our code will skip that key. – Deepak Singh Apr 05 '20 at 07:23
  • But then if we don't know against which keys:values we are testing it won't be the right test case. I would feel that the contract should always be up to date else the testing will never be completely certain. For the usecase mentioned in question if while parsing file and populating the POJO he still have keys left, it should simply fail the test that JSON structure has changed. Classical TDD approach. – Rupesh Apr 05 '20 at 07:30
  • I accept your point but in some use cases, we don't have to validate the whole data it just has to be a valid JSON. like at the time of inserting data in Mongo we can have dynamic data that can have any key, it just has to be a valid JSON. – Deepak Singh Apr 05 '20 at 07:39