0

Am getting response from ElasticSearch, from that response i want to form another JSON with limited fields (like custom JSONObject).

Please find the response that am getting from elastic search.

{ "took":93, "timed_out":false, "_shards":{ "total":5, "successful":5, "skipped":0, "failed":0 }, "hits":{ "total":1, "max_score":1.0, "hits":[ { "_index":"attachment", "_type":"doc", "_id":"87740", "_score":1.0, "_source":{ "app_language":"ES", "filetype":"PB", "attachment":{ "date":"2006-05-03T15:17:53Z", "content_type":"application/pdf", "author":"JJamesN", "language":"en", "title":"Microsoft Word - te7000pb.doc", "content":"European Electronic Controls Catalog ", "content_length":12267 }, "ext":"pdf", "fileContent":"JVBERi0xLjQNJeLjz9MNCjQ3ID" } } ] } } 

Please find my java code that am trying to manipulate the response JSON to create a separate JSON with limited fields.

JSONObject jsonObject = new JSONObject(responseBody); JSONObject hits = jsonObject.getJSONObject("hits"); JSONArray hitsArray=hits.getJSONArray("hits"); System.out.println("Hits---->"+hitsArray.toString()); 

From the response JSON, i just want to create a new JSON with the following Structure and fields.

 { "app_language":"ES", "filetype":"PB", "attachment.content" : "European Electronic Controls Catalog ", "ext":"pdf", } 

1 Answer 1

1

Try this:

JSONObject jsonBody = new JSONObject(); jsonBody.put("app_language","value"); jsonBody.put("filetype","value"); jsonBody.put("attachment.content","value"); jsonBody.put("ext",ext); //String requestBody = jsonBody.toString(); 

Just extract your values from response and use it any where you want. Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.