The condition for assigning to json_url is the wrong way round: currently for url: aaa.com/xxx we get: aaa.com/xxx&output=json instead of the correct value: aaa.com/xxx?output=json