Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java print XContentBuilder Json string

Tags:

java

I use an XContentBuilder object to build a Json string.

XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
                .field(Constants.OSM_ID, doc.getOsmId())
                .field(Constants.OSM_TYPE, doc.getOsmType())
                .field(Constants.OSM_KEY, doc.getTagKey())
                .field(Constants.OSM_VALUE, doc.getTagValue())
                .field(Constants.IMPORTANCE, doc.getImportance());

Now I would like to System.out the resulting Json String. How can I do this?

like image 942
Chris Avatar asked Feb 16 '16 21:02

Chris


2 Answers

since elastic client v6.3

import org.elasticsearch.common.Strings;

String json = Strings.toString(builder);

see elastic docs

like image 139
pulbere Avatar answered Nov 17 '22 21:11

pulbere


Finish building:

String s = builder.string();
System.out.println(s);
like image 7
wero Avatar answered Nov 17 '22 21:11

wero