The answer is simple, since you're handling this script on the
onclick
event it would always trigger that way. If you want it to happen as soon as the page loads you can try to add it to the body tag, like this
<body onload="testJson()"></body>
This would execute the very time when you will run the page to load it.
One more thing as Nirav has also mentioned would be to make sure the jQuery code is present inside the
$(document).ready(function () { ... });
block. But I don't think that is a problem around since the code works in the
click
event.