title: I want to add a property to JSON Object with an array of integers as a value in Java
body: <p>I want to create a JSON object that the value of one of its properties is an array, it should look as follow:</p>
  
  <pre><code>{
    "id": 1,
    "name": "Whatever",
    "indices": [1, 2, 3, 4]
  }
  </code></pre>
  
  <p>Running the following code:</p>
  
  <pre><code>private void createJsonWithArrayAsPropertyValue() throws IOException {
      JsonObjectBuilder job = Json.createObjectBuilder();
      job.add("id", 1);
      job.add("name", "no-name");
      job.add("indices", Json.createArrayBuilder()
              .add(1)
              .add(2)
              .add(3)
              .build());
      System.out.println(new StringBuffer("\tJSON String = ").append(job.toString()).append("\n"));
  
      JsonObject jsonObject = job.build();
      Writer writer = new StringWriter();
  
      //  region Print the JSON object into a String
      Json.createWriter(writer).write(jsonObject);
      StringBuffer jsonString = new StringBuffer(writer.toString());
      System.out.println(new StringBuffer("\tjsonString = ").append(jsonString).append("\n"));
      //  endregion
  
      //  region Print the JSON using PRETTY_PRINTING format
      Map&lt;String, Boolean&gt; config = new HashMap&lt;&gt;();
      config.put(JsonGenerator.PRETTY_PRINTING, true);
      JsonWriterFactory writerFactory = Json.createWriterFactory(config);
      writer = new StringWriter();
      writerFactory.createWriter(writer).write(jsonObject);
      jsonString = new StringBuffer(writer.toString());
      System.out.println(new StringBuffer("\tjsonString (PRETTY_PRINTING) = ").append(jsonString).append("\n"));
      //  endregion Print the JSON using PRETTY_PRINTING format
      System.out.println("-------------------------------------------------");
  
  }
  </code></pre>
  
  <p>Creates the JSON Object using <code>com.json.JSONObject</code> the <em>indices</em> property is entered as a string:</p>
  
  <pre><code>{
    "id": 1,
    "name": "Whatever",
    "indices": "[1, 2, 3]"
  }
  </code></pre>
  
  <p>I am looking for a solution that will not enter the element value as a string and I am not limited to use any specific package.</p>
  
  <p><strong>UPDATE</strong> Using <code>javax.json.Json</code> as such:</p>
  
  <pre><code>    jo.put("indices", Json.createArrayBuilder()
              .add(1)
              .add(2)
              .add(3)
              .build());
  </code></pre>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.2.0.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.beniregev&lt;/groupId&gt;
      &lt;artifactId&gt;demos_and_tutorials&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;demos_and_tutorials&lt;/name&gt;
      &lt;description&gt;Java 8 + Spring Boot project for demos and tutorials&lt;/description&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;spring-cloud.version&gt;Hoxton.RC1&lt;/spring-cloud.version&gt;
          &lt;javafaker.version&gt;0.17.2&lt;/javafaker.version&gt;
          &lt;mapdb.version&gt;3.0.7&lt;/mapdb.version&gt;
          &lt;android-json.version&gt;0.0.20131108.vaadin1&lt;/android-json.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-amqp&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-oauth2-client&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-oauth2-resource-server&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.session&lt;/groupId&gt;
              &lt;artifactId&gt;spring-session-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/javax.json/javax.json-api --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.json&lt;/groupId&gt;
              &lt;artifactId&gt;javax.json-api&lt;/artifactId&gt;
              &lt;version&gt;1.1.4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/org.glassfish/javax.json --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish&lt;/groupId&gt;
              &lt;artifactId&gt;javax.json&lt;/artifactId&gt;
              &lt;version&gt;1.1.4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.github.javafaker&lt;/groupId&gt;
              &lt;artifactId&gt;javafaker&lt;/artifactId&gt;
              &lt;version&gt;${javafaker.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.projectreactor&lt;/groupId&gt;
              &lt;artifactId&gt;reactor-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/org.mapdb/mapdb --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mapdb&lt;/groupId&gt;
              &lt;artifactId&gt;mapdb&lt;/artifactId&gt;
              &lt;version&gt;${mapdb.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.amqp&lt;/groupId&gt;
              &lt;artifactId&gt;spring-rabbit-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.vaadin.external.google&lt;/groupId&gt;
              &lt;artifactId&gt;android-json&lt;/artifactId&gt;
              &lt;version&gt;${android-json.version}&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;spring-milestones&lt;/id&gt;
              &lt;name&gt;Spring Milestones&lt;/name&gt;
              &lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>UPDATE 2:</strong> I added dependency for <em>org.glassfish</em> so <code>javax.json</code> can run, as it say on <a href="http://www.java2s.com/Tutorials/Java/JSON/0100__JSON_Java.htm" rel="nofollow noreferrer">Java JSON Tutorial - JSON Java</a> and also on <a href="https://javaee.github.io/jsonp/getting-started.html" rel="nofollow noreferrer">Java API for JSON Processing - Getting Started</a>.
  <strong>Update 3</strong> The exception I got because of a conflict between dependencies in my pom.xml, I moved my code to a separate project and <strong>it works</strong>.</p>
  
  <p>I receive an exception (I missing something in my pom.xml?):</p>
  
  <pre><code>javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
      at javax.json.spi.JsonProvider.provider(JsonProvider.java:99)
      at javax.json.Json.createArrayBuilder(Json.java:262)
      at com.beniregev.demos_and_tutorials.demos.JsonDemo.simpleJsonObjectWithArrayAsValue(JsonDemo.java:41)
      at com.beniregev.demos_and_tutorials.demos.JsonDemo.main(JsonDemo.java:57)
  Caused by: java.lang.ClassNotFoundException: org.glassfish.json.JsonProviderImpl
      at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
      at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
      at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
      at java.base/java.lang.Class.forName0(Native Method)
      at java.base/java.lang.Class.forName(Class.java:315)
      at javax.json.spi.JsonProvider.provider(JsonProvider.java:96)
      ... 3 more
  </code></pre>
  
  <p>I will appreciate any assistance.</p>