Hey guys,
I want to read or load json file directly from server and store json data in single object.
can somebody suggest some related code for that.
assume I have url like https://testdata.com/content/data.json
Note : this is fack url.
Hey guys,
I want to read or load json file directly from server and store json data in single object.
can somebody suggest some related code for that.
assume I have url like https://testdata.com/content/data.json
Note : this is fack url.
import groovy.json.JsonSlurperJsonSlurper jsl = new JsonSlurper()def parsedResponse = jsl.parse(new URL("https://jsonplaceholder.typicode.com/users"))println parsedResponse[0].id
Hello Marek Melocik
I have tried to parse json from these two urls. but in this case second url is parse suceessfully and
first one is not parse its throwing error.even if both urls have same content.
http://webhook.satyamphysio.in:77/Lang.Product-en.json
http://webhook.satyamphysio.in:77/Lang.UserProduct-en.json
my code look like this
static String readLanguageJson(String key) { JsonSlurper jsl = new JsonSlurper() RequestObject ro = new RequestObject() def parsedResponse = "" String defaultUrl = "http://webhook.satyamphysio.in:8081/Lang.Product-en.json" String customUrl = "http://webhook.satyamphysio.in:8081/Lang.UserProduct-en.json" ro.setRestRequestMethod("GET") ro.setRestUrl(defaultUrl) ResponseObject resp = WSBuiltInKeywords.sendRequest(ro) if (resp.getStatusCode() == 200) { parsedResponse = jsl.parse(new URL(defaultUrl)) } return parsedResponse[key]}
I have Error log like this
08-27-2018 10:42:30 AM - [ERROR] - Unable to determine the current character, it is not a string, number, array, or object
_The current character read is ‘’ with an int value of 65279
__Unable to determine the current character, it is not a string, number, array, or object
__line number 1
__index number 0
__{
__^
__08-27-2018 10:42:30 AM - [END] - End action : Statement - data = CustomKeywords.common.CommonClass.readLanguageJson(“Home”)
__08-27-2018 10:42:30 AM - [ERROR] - Test Cases/ViewBasket/TCBasket2-Check Multilingual FAILED because (of) groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
__The current character read is ‘’ with an int value of 65279
__Unable to determine the current character, it is not a string, number, array, or object
__line number 1
__index number 0
__{
_^
Hello, although they look the same, they are not.
_The current character read is ‘’ with an int value of 65279 _- this is invisible character called Byte Order Mark (https://stackoverflow.com/questions/6784799/what-is-this-char-65279) which Java cannot handle properly. Use this updated code, it should work when you replace Unicode representation of BOM.
JsonSlurper jsl = new JsonSlurper() RequestObject ro = new RequestObject() def myJson = "" def parsedResponse = "" String defaultUrl = "http://webhook.satyamphysio.in:8081/Lang.Product-en.json" String customUrl = "http://webhook.satyamphysio.in:8081/Lang.UserProduct-en.json" ro.setRestRequestMethod("GET") ro.setRestUrl(defaultUrl) ResponseObject resp = WSBuiltInKeywords.sendRequest(ro) if (resp.getStatusCode() == 200) { // replace BOM in response text myJson = resp.getResponseText().replaceAll("\uFEFF","") parsedResponse = jsl.parseText(myJson) }
thank you:)
hi @Marek_Melocik!! I’m kind of having the same problem, but see, this is the response I get from my call:
lt;html><body>{‘auth_code’: ‘1xxxxxxxxxxxxxxxxxxxxxxx’}</body></html>
I want to extract the auth_code value that the response returns, this is the code on my test case:
oauthcode = WS.sendRequest(findTestObject(‘REQ 1 Login (oauthgetcode)’))
def jsonSlurper = new JsonSlurper()
def jsonResponse = jsonSlurper.parseText(oauthcode.getBodyContent())
But it break with groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
The current character read is ‘<’ with an int value of 60
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0
<html><body>{‘auth_code’: ‘1xxxxxxxxxxxxxxxxxxxxxxx’}</body></html>
^
not quite sure how to implement the solution you gave in this thread but any help you can give me, will really appreciate.
Hello,
this is not a valid JSON string, therefore JSONSlurper throws an error here. For some reason, it is a mix of HTML and JSON format. To parse only JSON part:
String response = "<html><body>{‘auth_code’: ‘1xxxxxxxxxxxxxxxxxxxxxxx’}</body></html>"
String jsonPart = response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1)
And then you can use JSONSlurper.
def jsonSlurper = new JsonSlurper()
def jsonResponse = jsonSlurper.parseText(jsonPart)
Hi,
Below tutorial will discuss on how to load json from url response,from file and from variables