简体   繁体   中英

Gatling : regex to get a specific value from a JSON

I have this JSON Object coming from an HTML.

{
  "isCompany": false,
  "accommodations": [
    {
      "id": "00000000031000006435",
      "isChecked": false,
      "name": "Théo",
      "addressLine1": "Rue des patriotes 40 Y",
      "addressLine2": "1050 Ixelles",
      "nightsDeclared": 5,
      "schoolNightsDeclared": 3,
      "schoolNightsAttached": 0,
      "taxableNights": 2.0,
      "totalPayment": 0.0,
      "isInProgress": true,
      "isLate": false,
      "isPayed": "false",
      "deadline": "2021-12-31",
      "initialAmount": 0.0,
      "remainingAmount": 0.0
    },
    {
      "id": "00000000031000006438",
      "isChecked": false,
      "name": "Théo",
      "addressLine1": "Rue des Gens 45 B",
      "addressLine2": "1040 Etterbeek",
      "nightsDeclared": 5,
      "schoolNightsDeclared": 3,
      "schoolNightsAttached": 0,
      "taxableNights": 2.0,
      "totalPayment": 0.0,
      "isInProgress": true,
      "isLate": false,
      "isPayed": "false",
      "deadline": "2021-12-31",
      "initialAmount": 0.0,
      "remainingAmount": 0.0
    }
  ]
}

I know that in Gatling, it is possible to get the accommodation id by writing this regex:

check(regex(""""accommodations":\[\{"id":"(.*?)"""").saveAs("accommodationId"))

Now my question is, what is the regex that gets the "isInProgress" ?

Ok, you have this in your regex

"id":"(.*?)"

You need just change to expected key name as isInProgress or any another. Also pay attention on " around (.*?) - since the value for id a string, they are needed, but value in isInProgress with another type .

Don't!

Using regex in this specific case could result in your code breaking on slight input changes and will result in unreadable code.

Instead deserialize and access as a dictionary?

[a['id'] for a in json.loads(json_string)['accommodations']]

Also, have you tried to simply replace id with the name of the field you want?

If you insist on using regex for this, check out online regex sites like regex101.com, regexr.com, regextester.com etc. (search for "online regex test") and try to solve this yourself. If your code does not work, ask a question again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM