basel.ai
basel.ai

Reputation: 157

Create an empty json field using JAVA

I want to generate a JSON like this:

{ “index”:{} }

Here is the code I'm using:

JSONObject indexObject = new JSONObject( );
indexObject.put( "index", new JSONArray());

but it outputs this:

{"index":[]}

Upvotes: 2

Views: 6491

Answers (1)

Ilya Sereb
Ilya Sereb

Reputation: 2571

That happens, because you are putting JSONArray() there. Try putting JSONObject() just like that:

indexObject.put( "index", new JSONObject());

Thanks, @bhusak.

Upvotes: 3

Related Questions