I use solrj to submit a query to solr , that returns the result in json format.
SolrQuery query = new SolrQuery();
SolrQuery query = new SolrQuery();
query.setParam("kw", keyword);
query.setParam("lc", location);
query.setParam("wt", "json");
query.setParam(CommonParams.QT, "/lds");
QueryResponse qResponse = solrServer.query(query);
searchResultStr = qResponse.toString();
But the searchResultStr does not have a string in JSON format. Instead it has something like this:
{responseHeader={status=0,QTime=21},location={has_zipcode=true,location_param=94085}}
But if i directly hit the solr url in the browser, I get the correct JSBON format:
{"responseHeader":{"status":0,"QTime":15},"location": {"has_zipcode":true,"location_param":"94085"}}
For a JSON output you will have to query a HTTPSolrServer using curl as mentioned in the answer here. Using EmbeddedSolrServer will not help i.e. solrj.
When you use solrj to query you will have to get the SolrDocumentList from the QueryResponseobject and convert it into JSON format by iterating through each SolrDocument and entering the data into JSON the way you want.
Solr QueryResponse returns a hashmap, so use any hashmap to json converter to get the json out of it. Here I have used Gson() for that conversion.
QueryResponse response = solr.query(query);
Gson gson = new Gson();
String json = gson.toJson(response.getResults());
System.out.println(json);
Related
I have written a QLDB query to fetch a document by document ID So that I want to convert this document to a JSON response and pass it through the rest end point.
qldbDriver.QldbDriver().execute(txn ->{
IonSystem ionSys = IonSystemBuilder.standard().build();
Result result = txn.execute("SELECT * FROM _ql_committed_WALLET WHERE metadata.id = ?",ionSys.newString(id));
IonStruct person = (IonStruct) result.iterator().next();
String s = person.get("data").toPrettyString();
});
Here I want that conversation.
How can I fix this issue?
There are many ways to achieve what you are trying to do. But picking up from your example, you might want to convert your result person into JSON directly, or you might want to use a library to generate that JSON. If it possible to convert from IonValue (of which IonStruct is an instance) to POJOs and then you could convert those to JSON using Jackson.
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
...
IonValue personValue = qldbDriver.QldbDriver().execute(txn ->{
IonSystem ionSys = IonSystemBuilder.standard().build();
Result result = txn.execute("SELECT * FROM _ql_committed_WALLET WHERE metadata.id = ?",ionSys.newString(id));
return (IonStruct) result.iterator().next();
});
Person person = IonObjectMapper.builder().build().readValue(personValue, Person.class);
String personJson = new ObjectMapper().writeValueAsString(person);
In this example we are taking the IonValue as returned from QLDB and converting it to a POJO using the Jackson Ion library. Then we use the regular JSON Jackson library to convert that same Person POJO into a JSON string which you can then send over your REST connection as the body of the response.
I have a set of input data files in JSON and I am trying to replace a value present in a JSON file and use that value to do a post request in restAssured
The JSON file has
{
"items": [
{
"item_ref": 241,
"price": 100
}
]
}
jsonbody below is a String of the above JSON file
This is the code that fails:
JSONObject jObject = new JSONObject(jsonbody);
jObject.remove("item_ref");
jObject.put("item_ref","251");
System.out.println(jObject);
This is what I am getting:
{"item_ref":"251","items":[{"item_ref":241,"price":100}]}
What I want is {"items":[{"item_ref":251,"price":100}]}
I also tried
JSONObject jObject = new JSONObject(jsonbody);
jObject.getJSONObject("items").remove("item_ref");
jObject.getJSONObject("items").put("item_ref","251");
System
But it says JSONObject["items"] is not a JSONObject.
All I need is to replace the 241 with 251. Is there an easier way to do this?
In general if we have a predefined JSON body file and if we want to replace some of the values in the body and use that in our POST calls within RestAssured, is there any easier way to do it?
The problem is - field item_ref and price are not in JSON Object as you think they are.
They are in JSON Array which contains JSON Objects. In order to modify that value, you have to get elements of the array and THEN execute very similar code you wrote.
Check this out:
JSONObject jObject = new JSONObject(jsonbody);
JSONArray array = jObject.getJSONArray("items");
JSONObject itemObject = (JSONObject) array.get(0); //here we get first JSON Object in the JSON Array
itemObject.remove("item_ref");
itemObject.put("item_ref", 251);
The output is:
{"items":[{"item_ref":251,"price":100}]}
Also, you can create a Hashmap:
HashMap<String,String> map = new HashMap<>();
map.put("key", "value");
RestAssured.baseURI = BASE_URL;
RequestSpecification request = RestAssured.given();
request.auth().preemptive().basic("Username", "Password").body(map).put("url");
System.out.println("The value of the field after change is: " + map.get("key"));
I want to convert the elasticsearch search result to Json Object. I havent found any proper way to convert directly.
SearchResponse response = client.prepareSearch(index).setExplain(true).execute().actionGet();
response->JSON Object.
Is there any way to convert an ElasticSearch response to a Json Object?
In Java, you can directly convert the SearchResponse to JSONObject.
Below is the handy code.
SearchResponse SR = builder.setQuery(QB).addAggregation(AB).get();
JSONObject SRJSON = new JSONObject(SR.toString());
You need to use the SearchResponse.toXContent() method like this:
SearchResponse response = client.prepareSearch(index).setExplain(true).execute().actionGet();
XContentBuilder builder = XContentFactory.jsonBuilder();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
JSONObject json = new JSONObject(builder.string());
I am using Mule 3.6.1 and in datamapper I have a JSON object which is a string datatype and I need to get the value of a field from the JSON object.
How can I get this value from the JSON object while the object is of type String?
I cannot use the JSON transformer for this.
Thanks for any help
To convert a String of JSON and get one of its field value inside DataMapper, then you can utilize code like this (in DataMapper Script area):
jsonObject = new org.json.JSONObject(input.jsonstring);
output.jsonValue = jsonObject.getString("jsonfield");
In order to convert JSON element to a series of objects. Google GSon library is very helpful.
Example:
import com.google.gson.Gson;
Gson gson = new Gson();
Student studentTest = gson.fromJson(data, Student.class);
System.out.println("Amount: " + studentTest .getStudentName());
What is the best way to retrieve data from JSON field in PostgreSQL using POCO framework?
The only way I see is:
Poco::Data::RecordSet rs(session, sql);
rs.moveFirst();
string value = rs[0].convert<std::string>(); // get JSON as string
Poco::JSON::Parser parser;
parser.parse(value);
Poco::Dynamic::Var result = parser.result();
// now we can extract Object, Array and so on
Direct extract
Poco::JSON::Object object = os[0].extract<Poco::JSON::Object>()
throws Can not convert [ERRFMT] to [ERRFMT] exception.
Any better solution?
Var result = parser.parse(value);
Object::Ptr object = result.extract<Object::Ptr>();