9951 explained code solutions for 126 technologies


phpunitHow to use PHPUnit json assert?


PHPUnit json assert is a method used to compare two JSON strings. It can be used to check if two JSON strings are equal or not.

Example code

$json1 = '{"name":"John","age":30,"city":"New York"}';
$json2 = '{"name":"John","age":30,"city":"New York"}';
$this->assertJsonStringEqualsJsonString($json1, $json2);

Output example

OK

Code explanation

  • $json1 and $json2: two JSON strings to be compared
  • assertJsonStringEqualsJsonString(): the method used to compare two JSON strings

Helpful links

Edit this code on GitHub