Home > Mobile >  Gatling : get the first element of the first object in a JSON Array
Gatling : get the first element of the first object in a JSON Array

Time:12-18

I have this JSON object :

{"isCompany":false,"accommodations":[{"id":"00000000031000000067","isChecked":false,"name":"5 JULI 2017","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","nightsDeclared":0,"schoolNightsDeclared":0,"schoolNightsAttached":0,"taxableNights":0.0,"totalPayment":0.0,"isInProgress":false,"isLate":false,"isPayed":"false","deadline":"2021-12-31","initialAmount":0.0,"remainingAmount":0.0},{"id":"00000000031000006362","isChecked":false,"name":"BELLEVIE","addressLine1":"STRAAT 10 ","addressLine2":"1000 New York","nightsDeclared":0,"schoolNightsDeclared":0,"schoolNightsAttached":0,"taxableNights":0.0,"totalPayment":0.0,"isInProgress":false,"isLate":false,"isPayed":"false","deadline":"2021-12-31","initialAmount":0.0,"remainingAmount":0.0}]}

Which if prettified, render this :

{
  "isCompany": false,
  "accommodations": [
    {
      "id": "00000000031000000067",
      "isChecked": false,
      "name": "5 JULI 2017",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "nightsDeclared": 0,
      "schoolNightsDeclared": 0,
      "schoolNightsAttached": 0,
      "taxableNights": 0.0,
      "totalPayment": 0.0,
      "isInProgress": false,
      "isLate": false,
      "isPayed": "false",
      "deadline": "2021-12-31",
      "initialAmount": 0.0,
      "remainingAmount": 0.0
    },
    {
      "id": "00000000031000006362",
      "isChecked": false,
      "name": "BELLEVIE",
      "addressLine1": "STRAAT 10 ",
      "addressLine2": "1000 New York",
      "nightsDeclared": 0,
      "schoolNightsDeclared": 0,
      "schoolNightsAttached": 0,
      "taxableNights": 0.0,
      "totalPayment": 0.0,
      "isInProgress": false,
      "isLate": false,
      "isPayed": "false",
      "deadline": "2021-12-31",
      "initialAmount": 0.0,
      "remainingAmount": 0.0
    }
  ]
}

I've got this full JSON Array from a div inside of an HTML by writing this :

.check(css("section.ht-declarations-tab-content-container>div#DATA--DECL-DATA").saveAs("jsonObj"))

And then to render the result I wrote this :

.exec { session => println("json = "   session("jsonObj").as[String]); session }.exitHereIfFailed

However as explained, I've got the full JSON array.

How to do to just get the first ID element of the first object? So basically : 00000000031000000067

CodePudding user response:

I would use Jackson for that job:

private static final ObjectMapper MAPPER = new ObjectMapper();

public static String getFirstId(String json) throws JsonProcessingException {
    return MAPPER.readTree(json).get("accommodations").get(0).get("id").asText();
}

Then you can do:

System.out.println(getFirstId(json));

Output:

00000000031000000067

If you want to print de JSON as a tree you can do:

public static String toTree(String json) throws JsonProcessingException {
    return MAPPER.readTree(json).toPrettyString();
}

Then you can do:

System.out.println(toTree(json));

Output:

{
  "isCompany" : false,
  "accommodations" : [ {
    "id" : "00000000031000000067",
    "isChecked" : false,
    "name" : "5 JULI 2017",
    "addressLine1" : "STRAAT 10 ",
    "addressLine2" : "1000 New York",
    "nightsDeclared" : 0,
    "schoolNightsDeclared" : 0,
    "schoolNightsAttached" : 0,
    "taxableNights" : 0.0,
    "totalPayment" : 0.0,
    "isInProgress" : false,
    "isLate" : false,
    "isPayed" : "false",
    "deadline" : "2021-12-31",
    "initialAmount" : 0.0,
    "remainingAmount" : 0.0
  }, {
    "id" : "00000000031000006362",
    "isChecked" : false,
    "name" : "BELLEVIE",
    "addressLine1" : "STRAAT 10 ",
    "addressLine2" : "1000 New York",
    "nightsDeclared" : 0,
    "schoolNightsDeclared" : 0,
    "schoolNightsAttached" : 0,
    "taxableNights" : 0.0,
    "totalPayment" : 0.0,
    "isInProgress" : false,
    "isLate" : false,
    "isPayed" : "false",
    "deadline" : "2021-12-31",
    "initialAmount" : 0.0,
    "remainingAmount" : 0.0
  } ]
}

CodePudding user response:

If your JSON is embedded inside HTML, you have 2 possibilities:

  1. Use a css check like you've done so far to extract the JSON from the HTML and then use transform to parse the JSON and extract the desired value, eg with Jackson as suggested in the other answer
  2. Use regex to extract the desired value in one go, eg check(regex(""""accommodations":\[\{"id":"(.*?)""""))
  • Related