Is there an easy way to convert properties with dot notation to json
I.E
server.host=foo.bar
server.port=1234
TO
{
Try reading this http://www.oracle.com/technetwork/articles/java/json-1973242.html, you will find several class to work with json.
I guess that retrieving the json from a local file, inner resource in the jar, or in another location specificable by an URL and the just read it with a JsonReader get the job dones.
This is a snipped from the reference site posted before.
URL url = new URL("https://graph.facebook.com/search?q=java&type=post");
try (InputStream is = url.openStream();
JsonReader rdr = Json.createReader(is)) {
JsonObject obj = rdr.readObject();
// from this line forward you got what you want :D
}
}
Hope it helps!
You can try with https://github.com/mikolajmitura/java-properties-to-json
You can generate Json from:
code example:
import pl.jalokim.propertiestojson.util.PropertiesToJsonConverter;
...
Properties properties = ....;
String jsonFromProperties = new PropertiesToJsonConverter().convertToJson(properties);
InputStream inputStream = ....;
String jsonFromInputStream = new PropertiesToJsonConverter().convertToJson(inputStream);
Map<String,String> mapProperties = ....;
String jsonFromInputProperties = new PropertiesToJsonConverter().convertToJson(mapProperties);
Map<String, Object> valuesAsObjectMap = ....;
String jsonFromProperties2 = new PropertiesToJsonConverter().convertFromValuesAsObjectMap(valuesAsObjectMap);
String jsonFromFilePath = new PropertiesToJsonConverter().convertPropertiesFromFileToJson("/home/user/file.properties");
String jsonFromFile = new PropertiesToJsonConverter().convertPropertiesFromFileToJson(new File("/home/user/file.properties"));
maven dependency:
<dependency>
<groupId>pl.jalokim.propertiestojson</groupId>
<artifactId>java-properties-to-json</artifactId>
<version>5.1.0</version>
</dependency>
dependency required minimum java 8.
more example of uses on https://github.com/mikolajmitura/java-properties-to-json
I didn't want any dependency on gson and I wanted to return a hierarchical json from a Spring controller so a deep Map was enough for me.
This works for me, just loop over all your keys and pass in an empty map.
void recurseCreateMaps(Map<String, Object> currentMap, String key, String value) {
if (key.contains(".")) {
String currentKey = key.split("\\.")[0];
Map<String, Object> deeperMap;
if (currentMap.get(currentKey) instanceof Map) {
deeperMap = (Map<String, Object>) currentMap.get(currentKey);
} else {
deeperMap = new HashMap<>();
currentMap.put(currentKey, deeperMap);
}
recurseCreateMaps(deeperMap, key.substring(key.indexOf('.') + 1), value);
} else {
currentMap.put(key, value);
}
}
Personally I used this dependency:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-properties</artifactId>
<version>2.10.2</version>
<scope>compile</scope>
</dependency>
private static JavaPropsMapper javaPropsMapper = JavaPropsMapper.builder().build();
final JsonNode request = javaPropsMapper.readPropertiesAs(parameters, JsonNode.class);
Try to use schema validation to avoid possible issues in you mapping.
Although, with a simple object mapper it is possible also
Properties properties = new Properties();
properties.put("array", new int[] {1,2,3});
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValueAsString(parameters);
Output
{"fullname":["Foo","Bar"]}