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());
Related
I have a String (jsonData) being mapped to json via Jackson Object mapper as below to JaxB.
var completeJson = objectMapper.readValue(jsonData, Data.class);
myDto.setFirstName(completeJson.getFirstName())
myDto.setLastName(completeJson.getLastName()))
.....
.....etc..etc
I'm able to map to the above strings just fine. However, I'm having problems mapping
to a jooq JSON object. I guess I will now have to convert jsonData to jooq JSON.
How would I do this?
JSON newJson = objectMapper.(best method to use);
myDto.setJsonSource(newJson)
Or maybe I have to create some sort of wrapper?
DTO configured by jooq
public myDto setJsonSource(JSON jsonSource) {
this.jsonSource = jsonSource;
return this;
}
The org.jooq.JSON type is just a wrapper around a string, so why not just use:
JSON json = JSON.json(objectMapper.writeValueAsString(object));
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>();
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);
I have a JSON URL I need to parse, How can we do that?
all the info you need on how to parse json is described here:
http://www.json.org/
http://developer.android.com/reference/org/json/JSONObject.html
For an object:
JSONObject json = new JSONObject(json_string);
For an array:
JSONArray json = new JSONArray(json_string);