First off all you need a json libary for Java.
So if you’re using this json libary you can do something like:
JSONObject obj = new JSONObject();
obj.put("lang", "python");
obj.put("file", "class.py");
obj.toString()
will return {"file":"class.py","lang":"python"}
solved Dynamic JSON field create with java