-1
[
    {
        "id": "abd88c05-2919-45f2-b1d0-c99af140bbb0",
        "order_id": "2ddc8302-34e0-437c-8e77-0e0ca80c18ad",
        "user_id": "00000000-0000-0000-0000-000000000001",
        "prescription": "{\"prescription\":[{\"Dosage\":\"2-0-2\",\"Medicine\":\"Hair Ras\",\"composition\":\"\",\"description\":\"Boosts blood circulation to hair follicles\",\"info\":\"Take after meals\",\"is_recommended\":false,\"quantity\":\"2\",\"type\":\"ayurveda\"}]}",
        "created_at": "2022-10-14T13:43:47.039Z",
        "updated_at": "2022-10-14T13:43:47.039Z",
        ...
    }
]

JsonPath js_prescrition = newJsonPath(response_prescription);
Object prescription = js_prescrition.get("prescription");
JsonPath js_prescrition1 = new JsonPath("prescription");
String medicine = js_prescrition.get("Medicine");
System.out.println(medicine);
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    The current state of the question is very poor, consider improving it. Firstly, please describe what have you tried? Share your **code-attempt** and describe the issue you've encountered. Secondly, **simplify** your JSON. Use *edit* button or this [link](https://stackoverflow.com/posts/74071620/edit). – Alexander Ivanchenko Oct 14 '22 at 16:20
  • Read [ask] and edit the question to make it more answerable. The amount of effort you put into asking the question is directly related to the quality of answers you will get. – E-Riz Oct 14 '22 at 18:08
  • [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Abra Oct 15 '22 at 04:54

2 Answers2

0

This code would help you

String data = given()...asString();
String prescription = JsonPath.from(data).getString("[0].prescription").replaceAll("\\\\", "");
List<Map<String, ?>> nested = JsonPath.from(prescription).get("prescription");
String medicine = (String) nested.get(0).get("Medicine");
System.out.println(medicine);
//Hair Ras
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
0

Library Josson can do it in a short query statement.

https://github.com/octomix/josson

Josson josson = Josson.fromJsonString(
    "[" +
    "    {" +
    "        \"id\": \"abd88c05-2919-45f2-b1d0-c99af140bbb0\"," +
    "        \"order_id\": \"2ddc8302-34e0-437c-8e77-0e0ca80c18ad\"," +
    "        \"user_id\": \"00000000-0000-0000-0000-000000000001\"," +
    "        \"prescription\": \"{\\\"prescription\\\":[{\\\"Dosage\\\":\\\"2-0-2\\\",\\\"Medicine\\\":\\\"Hair Ras\\\",\\\"composition\\\":\\\"\\\",\\\"description\\\":\\\"Boosts blood circulation to hair follicles\\\",\\\"info\\\":\\\"Take after meals\\\",\\\"is_recommended\\\":false,\\\"quantity\\\":\\\"2\\\",\\\"type\\\":\\\"ayurveda\\\"}]}\"," +
    "        \"created_at\": \"2022-10-14T13:43:47.039Z\"," +
    "        \"updated_at\": \"2022-10-14T13:43:47.039Z\"" +
    "    }" +
    "]");
    
String medicine = josson.getString("[0].json(prescription).prescription[0].Medicine");
System.out.println(medicine);
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8