When I try to parse a swagger using the path $..[description,title] I'm getting Invalid Path Exception.But I'm able to get the response in when I try to evaluate the same in jsonpath.com
enter image description here
Related
I want to get the path for a Json property and I am using the Json Path for parsing an Object in order to find the value of the property.
Unfortunately, I encounter the error: Expected to find an object with property ['id'] in path $ but found 'java.lang.String'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
My code is below:
String path = "$.id";
return JsonPath.parse(document).read(path);
The json from which I want to extract the data (If I evaluate the JsonParse.parse(document)), is:
id:"12345678"
eventName:"MAINTAIN"
ind_Card:0
I am interested in getting the String value of the id. What am I doing wrong here?
I am using a Meme generator API. My goal is to generate memes with the API , be able to view and save them as JPG images.
When I try to use the Java code provided by the creator, I get an error message.
Here's the provided code that fails:
HttpResponse<JsonNode> response = Unirest.get("https://ronreiter-meme-generator.p.rapidapi.com/meme?font=Impact&font_size=50&meme=Condescending-Wonka&top=Yolo&bottom=HA")
.header("X-RapidAPI-Key", "TOP_SECRET")
.asJson();
The error message:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'APIController' defined in file
[C:\yaml\out\production\classes\com\example\demo\controllers\APIController.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'APIService' defined in file
[C:\yaml\out\production\classes\com\example\demo\services\APIService.class]:
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [com.example.demo.services.APIService]: Constructor threw
exception; nested exception is
com.mashape.unirest.http.exceptions.UnirestException:
java.lang.RuntimeException: java.lang.RuntimeException:
org.json.JSONException: A JSONArray text must start with '[' at 1
[character 2 line 1]
It says the field
response
cannot be parsed as a JSONArray, so I tried this code snippet instead:
HttpResponse<String> meme = Unirest.get("https://ronreiter-meme-generator.p.rapidapi.com/meme?font=Impact&font_size=50&meme=Impossibru-Guy-Original&top=Random+meme&bottom=Bottom+text")
.header("X-RapidAPI-Key", "TOP_SECRET")
.asString();
In this case the code runs, but as I call the endpoint, I get loads of
ufffd
snippets in the String, which basically means that I am trying to read a code which has no representation in Unicode. I've seen a solution here how I could deal with this problem but I'm not pretty sure I'm going on a right way.
According to website, on which the API is provided, I should get something like this as a response:
Could you give me any pieces of advice how to approach this problem?
Thanks for your help in advance.
The content-type of your API specification contains "image/jpeg".
This means that the response does not contain JSON, but binary image data, so trying to parse it as JSON will result in failure.
Try saving the response from your API directly to a file, you'll see that it's an image.
Eventually I could sort out the problem with some help.
Here it is:
HttpResponse httpResponse = Unirest.get("https://ronreiter-meme-generator.p.rapidapi.com/meme?font=Impact&font_size=50&meme=Condescending-Wonka&top=Top+text&bottom=Bottom+text")
.header("X-RapidAPI-Key", "YOUR_SECRET_API_KEY")
.asBinary();
InputStream inputStream = httpResponse.getRawBody();
BufferedImage imBuff = ImageIO.read(inputStream);
String filePath = "C:/x/x.jpg";
File file = new File(filePath);
ImageIO.write(imBuff, "jpg", file);
So here are the points to do:
Retrieve the response as binary data
Transform it to an InputStream
Create a BufferedImage from it
Create a File with the specified filePath
Write the BufferedImage into the FIle
I am trying to validate API Response with Json Schema File.
code snippet:
File schemaFile = new File("file\name\in\String\format");
isTrue("String message", JSONValidationUtil.isJsonValid(schemaFile, responseBody.asString()));
JsonValdationUtil is custom class written which contain methods for schema validation, and responseBody(RestAssured) is my response object after hitting rest api.
My code is integrated with sonarQube server and once I commit my code I am getting below error from sonarqube.
Path variable is not sanitized using getCanonicalPath()
I used file.getCanonicalPath() and file.getCanonicalFile() methods to sanitize my file path, But still I am getting same error.
The structure of my java web project is like this:
When I use this code to get the json file,error message said "Caused by:
java.io.FileNotFoundException:
....\resources\properties\highcharts_startYearWeek.json"
How can I get this JSON file and how to modify my code?
I use this code,the problem has been solved.
String jsonPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
jsonPath = jsonPath + "properties/";
startYearWeek = JsonUtil.parseJsonFile(jsonPath+"highcharts_startYearWeek.json", "{startYearWeek}");
System.out.println("startYearWeek:"+startYearWeek);
I want to load my Json file into neo4j server using Apoc procedure, for that I used following query...
call apoc.load.json("file:///training.json") yield value Return value
but it is showing me the error
"Failed to invoke procedure apoc.load.json: Caused by:
java.lang.RuntimeException: Can't read url file:///training.json as
json: \training.json (The system cannot find the file specified)".
Expected behaviour is Specified json file is to be loaded in to neo4j server.
problem is: It is not at all recognizing the file That is specified in URL.
please help me out to resolve this error
call apoc.load.json("file:///training.json") yield value Return value
for the above query the URL is specified as "file:///training.json". Instaed of this Specify full path name in URL.
for eg: I put my json file named "training.json" in following specified path
C:\Users\TEMP.DESKTOP9FCLQ6J.002\Documents\Neo4j\default.graphdb\import\training.json
so in query specify the above full path name. Instead of C:// specify the protocol name file:///
The correct answer is:
call apoc.load.json("file:///Users\\TEMP.DESKTOP-9FCLQ6J.002\\Documents\\Neo4j\\default.graphdb\\import\\test.json") yield value
Return value.
Thanks.
For the above call apoc.load.json, I'd use a backslash to get it working, on a Windows 10 PC:
WITH "file:///Users/mukundan/Documents/Neo4j/default.graphdb/import/sudish-graph_working1.json"
AS url
CALL apoc.load.json(url)
YIELD value