4

We have a requirement to create JSON object from the various JSONPaths that are provided. For example below are two paths and the values of that path that are there in the new JSON object to be created.

$.student.firstName = "Abc"
$.student.subject['physics'].mark=100

Is there any java open source library which helps to create the result object just from this path?

{
 "student":{
 "firstName":"Abc",
 "physics":{
   "mark":100
  }
 }
}

We explored certain libraries like JSONPath. It has option to parse the JSON file, but doesn't have option to recursively create the JSON file from the path.

4
  • Have you tried something yet ? Maybe this post can be interesting for you : stackoverflow.com/q/51971642/1140748 Commented Mar 10, 2021 at 8:45
  • JSON Path itself is a query syntax. It's not intended for transformations or building a new object. I don't believe JSON Path is the technology you're looking for. Are you starting with an existing JSON object? Commented Mar 10, 2021 at 9:05
  • I understand JSONPath just gives the path in a JSON. I wanted to create a new JSON from the various paths.. so that after I complete the path evaluation and object creation based on that, I have a complete JSON Object with me with data Commented Mar 10, 2021 at 10:22
  • @alain.janinm, had seen this example. It is using JsonPointer. It has limited options compared to JsonPath. Commented Mar 10, 2021 at 11:54

3 Answers 3

4

Based in this fork from JsonPath that add the feature to create a new json node from a json path, you can do something like this:

//    columnsMap = key: jsonPath node, value: value to be added to node

String json = "{ }";
for (Entry<String, String> entry : columnsMap.entrySet()) {
  JsonPath compiledPath = JsonPath.compile(entry.getKey());
  Object parsedJsonPath =
      compiledPath.set(configuration.jsonProvider().parse(json), entry.getValue(), configuration);
  json = JsonPath.parse(parsedJsonPath).jsonString();
}
return json;

Input (columnsMap):

{vendor_account_ids=1234567,4567123,789785,
 charges[0].vendor_charge_id=CHARGE-01,
 charges[1].vendor_charge_id=CHARGE-02,
 charges[0].type=LOAN_DEDUCTION,
 charges[0].conditions.alternative_delivery_date=false,
 charges[0].conditions.depends_on_vendor_charge_ids[0]=ID01,
 charges[0].conditions.depends_on_vendor_charge_ids[1]=ID02,
 charges[0].conditions.order_total.maximum_value=123.45,
 charges[0].conditions.payment_method=BANK_SLIP,
 charges[0].conditions.scaled_from=ORDER_TOTAL,
 charges[0].conditions.product_attributes.is_alcoholic=false,
 charges[0].conditions.payment_terms[0]=1,
 charges[0].conditions.payment_terms[1]=30,
 charges[0].output.scope=LINE_ITEM,
 charges[0].output.apply_to=DISCOUNT_AMOUNT,
 charges[0].output.type=PERCENT,
 charges[0].output.ratio=FIXED,
 charges[0].output.ranges[0].from=1,
 charges[0].output.ranges[0].value=10,
 charges[0].output.vendor_item_ids[0]=SKU001,
 charges[0].output.vendor_item_ids[1]=SKU002
}

Output (returned json):

{
    "vendor_account_ids": "1234567,4567123,789785",
    "charges": [
        {
            "vendor_charge_id": "CHARGE-01",
            "type": "LOAN_DEDUCTION",
            "conditions": {
                "alternative_delivery_date": "false",
                "depends_on_vendor_charge_ids": [
                    "ID01",
                    "ID02"
                ],
                "order_total": {
                    "maximum_value": "123.45"
                },
                "payment_method": "BANK_SLIP",
                "scaled_from": "ORDER_TOTAL",
                "product_attributes": {
                    "is_alcoholic": "false"
                },
                "payment_terms": [
                    "1",
                    "30"
                ]
            },
            "output": {
                "scope": "LINE_ITEM",
                "apply_to": "DISCOUNT_AMOUNT",
                "type": "PERCENT",
                "ratio": "FIXED",
                "ranges": [
                    {
                        "from": "1",
                        "value": "10"
                    }
                ],
                "vendor_item_ids": [
                    "SKU001",
                    "SKU002"
                ]
            }
        },
        {
            "vendor_charge_id": "CHARGE-02"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

0
public static JSONObject extractJsonByJsonPath(JSONObject object, Map<String, String> map) {
    JSONObject jsonObject = new JSONObject();

    if (CollUtil.isEmpty(object) || CollUtil.isEmpty(map)) {
        return jsonObject;
    }
    for (Map.Entry<String, String> entry : map.entrySet()) {

        JSONPath jsonPath = JSONPath.of(entry.getKey());
        jsonPath.set(jsonObject, JSONPath.eval(object, entry.getValue()));
    }
    return jsonObject;
}

You can Create JSON Object from old json like this, the map that key is new JsonPath,value is old JsonPath.I used fastjson2 library.

Comments

-1

look at Jayway Jsonpath, it has JSON generation function.

you should use "feature/transformation-api" branch to make use of transfomation api.

following rule make exactly the same json as you posted

{
"pathMappings": [{
        "target": "$.student.firstName",
        "additionalTransform": {
            "constantSourceValue": "Abc"
        }
    },
    {
        "target": "$.student.physics.mark",
        "additionalTransform": {
            "constantSourceValue": 100
        }
    }
]

}

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.