sonia sharma sonia sharma - 3 months ago 13
Javascript Question

How to compare json object with a string?

I am making a API call which will return JSON in the body and I want to compare JSON object with a String. How can I do that?

API

res.body
returns following JSON object:

{
"name": "tenant_tree",
"documents": [
{
"tags": [],
"scope": "all",
"tenant_id": "0",
"id": "9dd2c5a5-2de2-4ac8-b195-04290f83b6fb",
"version": 1,
"description": "",
"name": "0",
"grouping": "",
"perm_read": "all",
"perm_write": "all",
"version_author": "dg",
"version_date": 1470324147050,
"is_current": true,
"dependencies_guid": [""],
"dependencies_name": [""],
"display_name": "system"
},
}


I want to compare something like
expect(res.body).toMatch('"name": "0"');
but this fails.

Answer

I am able to compare json values in the API response body by:

expect(res.body.name).toEqual("0")

No need of parsing the response.

Refereed link : JSON.Parse,'Uncaught SyntaxError: Unexpected token o