简体   繁体   中英

Forming custom JSON Object from the JSON response from elastic search

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",
 }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM