I’m trying to get a JSON path for the HTML which has the language code DE
Here is the sample JSON
{
"name": "Name",
"text": "",
"html": "HTML content",
"tags": [],
"translations": [
{
"html": "HTML I don't want",
"text": "",
"language": {
"code": "AF",
"name": "AF"
}
},
{
"html": "HTML I want",
"text": "",
"language": {
"code": "DE",
"name": "German"
}
}
],
}
I’ve only been able to get the first translation by doing const afLang = requestPromptsJsonRequest.translations[0].html;
within Cypress. I’m not sure how to do a where clause to take the HTML relevant to the language code and not just the number in the translations JSON
3
Answers
here is the TypeScript code for the same:
Instead of using JSON Path, you can use
Array.find()
to retrieve thetranslations
object with the correct language code.You could also abstract that variable assignment to a function.
Cypress has a version of lodash attached which has a find method that can take a pattern to match
In the test:
Runnable example: