0

I'm creating a Spring application on backend and my main goal is to manage properties (add/update/delete) in *.properties file. I want to convert this file to JSON and then manipulate it from UI application.

Is there any possibility to convert structure like this:

a.x=1
a.y=2
b.z=3

To JSON like this:

{
    "a": {
        "x": 1,
        "y": 2
    },
    "b": {
        "z": 3
    }
}

I found solution to use GSON library, but it creates for me flat structure, not hierarchical, code I used:

Properties props = new Properties();
try (FileInputStream in = new FileInputStream(classPathResource.getFile())) {
    props.load(in);
}
String json = new GsonBuilder().enableComplexMapKeySerialization().create().toJson(props);

Is here someone who was facing same problem and maybe found a working project for this? Maybe GSON library can do that?

3
  • I have added an answer let me know if it works for you. Commented Oct 8, 2019 at 5:48
  • Maybe you can try with https://github.com/mikolajmitura/java-properties-to-json. Commented Oct 8, 2019 at 6:34
  • Thank you for answering and for those suggestions. Commented Oct 8, 2019 at 9:00

1 Answer 1

1

This solution does involve loads of work, but you will get what you want to achieve using the below code, basically, the idea is to split the key based on the single dot and then create a JsonObject if the same first key is found.

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import org.json.JSONObject;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class SOTest {
    public static void main(String[] args) throws IOException {
        JSONObject jsonObject = new JSONObject();
        FileReader fileReader = new FileReader(new File("C:\\Usrc\\main\\java\\Sample.properties"));
        Properties properties = new Properties();
        properties.load(fileReader);
        Iterator<Entry<Object, Object>> iterator = properties.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<Object, Object> entry =  iterator.next();
            String value = (String) entry.getKey();
            String[] values = value.split("\\.");

            JSONObject opt = jsonObject.optJSONObject(values[0]);
            if(opt!=null) {
                opt.put(values[1],entry.getValue());
            }else {
                JSONObject object = new JSONObject();
                object.put(values[1], entry.getValue());
                jsonObject.put(values[0], object);
            }       
        }

        System.out.println(jsonObject.toString());  
    }
}

Output

{"a":{"x":"1","y":"3"},"b":{"z":"10"}}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answering. In free time, when I was waiting when someone would answer I found on github really interesting project: github.com/lightbend/config and it nicely comes with handful functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.