1

There's a string like:

String query = "param1, param2, param3{npam1, npam2, npam3{nipam1, nipam2}}";

This string needs to be processed in the format:

{

    param1: param1, 
    param2: param2, 
    param3: {

        npam1: param3.npam1, 
        npam2: param3.npam2, 
        npam3: {

            nipam1: param3.npam3.nipam1, 
            nipam2: param3.npam3.nipam2

        }

    }

}

Have already done till 2 nested, but the point is the string to query can be extended to any number of nested curls.

1
  • 2
    I formatted your question a little. Hope your expected output is in format I assumed is expected. If not feel free to rollback my changes. Commented Jan 29, 2020 at 14:35

2 Answers 2

1

What I did was to iterate over the objects in the string and then iterate over the attributes of each object. Hopefully, it will help you to solve your problem. Also in your initial String, you are missing the open parenthesis and the close parenthesis so I added them.

    String jsonWithOutFormat = "param1, param2, param3{npam1, npam2, npam3{nipam1, nipam2}}";
    jsonWithOutFormat = jsonWithOutFormat.replaceAll(" ", "");
    String json = "";
    String[] objectsInString = jsonWithOutFormat.split("[{]");
    List<String> nestedObjects = new ArrayList<>();
    json += "{";
    for (int i = 0; i < objectsInString.length; i++) {
        String[] objectAttributes = objectsInString[i].split("[,]");
        if(i==0)
            nestedObjects.add(objectAttributes[objectAttributes.length-1] + ".");
        else
            nestedObjects.add(nestedObjects.get(i-1)+objectAttributes[objectAttributes.length-1] + ".");
        for (int j = 0; j < objectAttributes.length; j++) {
            if(!(j == objectAttributes.length-1)) {
                if(i != 0)
                    json+=  objectAttributes[j] + ": " +  nestedObjects.get(i-1) + objectAttributes[j]  + ", ";
                else
                    json+=  objectAttributes[j] + "\"" + ": " + "\"" + objectAttributes[j] + "\"" + ", ";
            }
            else {
                if(!(i == objectsInString.length-1))
                    json+=  objectAttributes[j] + ": {";
                else {
                    json+= objectAttributes[j].replaceAll("}", "")  + ": " + nestedObjects.get(i-1) + objectAttributes[j];
                }
            }
        }
    }
    json += "}";
    System.out.print("\n" + json);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. But instead of npam3: { nipam1: param3.npam3.nipam1, nipam2: param3.npam3.nipam2 } The Output is: npam3: { nipam1: nipam1, nipam2: nipam2 }
You are right, I add some changes to the code, let me know if that work for you
Thanks! It is working. But, as said if I insert a nested object like in this case: opam1 inside like: param1, param2, param3{npam1, npam2, npam3{nipam1{opam1}, nipam2}}, it results into: { npam3: { nipam1: {opam1} : param3.npam3.nipam1.opam1, nipam2: param3.npam3.nipam2 } Instead, in the key value of nested objects, no curls should be there.
It's not providing the same schema as the output for more deeper nested objects. Can you please help. Thanks!
Nice to heard that, you can edit the answer to share it with others.
|
1

Thanks Jorge. This method can be called to produce json in desired format, by passing the actual String (the one that is not formatted).

public String expressionConstruct(String jsonWithOutFormat) {

    jsonWithOutFormat = jsonWithOutFormat.replaceAll(" ", "");
    String json = "";
    String[] objectsInString = jsonWithOutFormat.split("[{]");
    List<String> nestedObjects = new ArrayList<>();
    json += "{";
    for (int i = 0; i < objectsInString.length; i++) {
        String[] objectAttributes = objectsInString[i].split("[,]");
        if(i==0)
            nestedObjects.add(objectAttributes[objectAttributes.length-1] + ".");
        else
            nestedObjects.add(nestedObjects.get(i-1)+objectAttributes[objectAttributes.length-1] + ".");
        for (int j = 0; j < objectAttributes.length; j++) {
            if(!(j == objectAttributes.length-1)) {
                if(i != 0)
                    json+=  objectAttributes[j].replaceAll("}", "") + ": " +  nestedObjects.get(i-1) + objectAttributes[j]  + ", ";
                else
                    json+=  objectAttributes[j] + ": " + objectAttributes[j] + ", ";
            }
            else {
                if(!(i == objectsInString.length-1))
                    json+=  objectAttributes[j] + ": {";
                else {
                    json+= objectAttributes[j].replaceAll("}", "")  + ": " + nestedObjects.get(i-1) + objectAttributes[j];
                }
            }
        }
    }
    json += "}";
    return json;

}

Comments

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.