-2

i am new to java and json and hence looking for your help. -i want to create a new json file provided few values should be taken from template(json).lets say i have json template as mentioned below. i want to extract what ever is there in step array .i.e step1 and step2. and this value should be put in new json.

problem i am facing 1)how to create nested json using jackson. i am using

 JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(new File("data/output.json"), JsonEncoding.UTF8);

generator.writeStartObject();
generator.writeObjectField("ABC","abc");
generator.writeEndObject();
generator.close();

but this will create a simple json ,not a nested json. i want nested json.

2)i want to extract step array and pass this to newly created json

i checked for this in stackoverflow ,found so many answers but those didnt help me. Hope some1 will help me.

json:

{
  "abc": "ryhey0",
  "shgsh": "jsdj",
  "fgjf": [
    {
      "type": "int",
      "steps": [
        {
          "type": "step1",
          "outputTypes": "int"
        },
        {
          "type": "step2",
          "outputTypes": "bool"
        }
      ]
    }
  ]
}
user6639252
  • 261
  • 1
  • 2
  • 9
  • 2
    Please clarify *create nested json*. – cassiomolin Jul 26 '16 at 10:54
  • create nested json means, i want to generate a new json(it is nested like the json template shon above ) using json – user6639252 Jul 26 '16 at 10:58
  • Do you mean "nested json" = nested json _objects_? Something like the object arrays in `fgjf` and `steps`? – Thomas Jul 26 '16 at 10:59
  • It's still unclear. – cassiomolin Jul 26 '16 at 10:59
  • Did you try creating a pojo for the template and the object you want to create? With those you could do template json -> template pojo, take the steps and put them into your result pojo and then use jackson to do result pojo -> json. – Thomas Jul 26 '16 at 11:00
  • no i want to create a json file in any folder.lets say i want a json file which looks like template file .i want to generate this using jackson. Also it should take few values from another json(template json ). – user6639252 Jul 26 '16 at 11:02
  • Actually i have given the code which i used to generate json file. But the problem is i dont know how to create json inside json(Nested json).Something like {"a":"b","c":{"d":"E"}} – user6639252 Jul 26 '16 at 11:03
  • @Thomas could you provide me an example for the same? – user6639252 Jul 26 '16 at 11:05
  • `{"a":"b","c":{"d":"E"}}` - this doesn't mean nested json _files_ but nested json _objects_. – Thomas Jul 26 '16 at 11:07

2 Answers2

0

Note: These are not tested. I have just demonstrated the approaches here

Approach 1 (Serializing object to JSON):

ObjectMapper mapper = new ObjectMapper();
ClassWithCollectionFieldsToRepresentHierarchy obj = new ClassWithCollectionFieldsToRepresentHierarchy();

//Serialize object to json into file
mapper.writeValue(new File("c:\\obj.json"), obj);

//Serialize object to json into string
String jsonInString = mapper.writeValueAsString(obj);

Approach 2 (Using Jackson API for building JSON):

ObjectMapper mapper = new ObjectMapper();
ObjectNode objectNode1 = mapper.createObjectNode();
objectNode1.put("abc", "ryhey0");
objectNode1.put("shgsh", "jsdj");
ArrayNode arrayNode2 = mapper.createArrayNode();
ObjectNode objectNode2 = mapper.createObjectNode();
objectNode2.put("type", "int");
ArrayNode arrayNode1 = mapper.createArrayNode();
ObjectNode objectNode3 = mapper.createObjectNode();
objectNode2.put("type", "step1");
objectNode2.put("outputTypes", "int");
ObjectNode objectNode4 = mapper.createObjectNode();
objectNode2.put("type", "step2");
objectNode2.put("outputTypes", "bool");
arrayNode1.add(objectNode3);
arrayNode1.add(objectNode4);
objectNode2.put("steps", arrayNode1);
arrayNode2.add(objectNode2);
objectNode1.put("fgjf", arrayNode2);
//Serialize object to json into file
mapper.writeValue(new File("c:\\obj.json"), objectNode1);

//Serialize object to json into string
String jsonInString = mapper.writeValueAsString(objectNode1);
Robin Rizvi
  • 5,113
  • 4
  • 27
  • 35
  • i have 1 more question: – user6639252 Jul 28 '16 at 06:49
  • i have a folder which have 'n' no of "Json" files.i want to get the name of these "json" files and also iterate through these files. – user6639252 Jul 28 '16 at 06:52
  • @user6639252 Stop asking new questions in the comments. If you have a new question, [ask a new question in the right place](http://stackoverflow.com/questions/ask) and we are going to help you. – cassiomolin Jul 28 '16 at 09:45
  • See this link and just replace extension to json - http://stackoverflow.com/questions/1384947/java-find-txt-files-in-specified-folder – Robin Rizvi Jul 28 '16 at 10:54
0

Note: Your question is not really clear and it's hard to provide you with a definitive answer until you provide some clarification regarding what you want to achieve. However, here are some pieces of code that you may find helpful:

Adding a new property to a JSON:

ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(new File("/path/to/your/json/file"));

((ObjectNode) root).put("new-property", "some-value");
System.out.println(root);

Getting the steps node:

ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(new File("/path/to/your/json/file"));

JsonNode steps = root.get("fgjf").get(0).get("steps");
System.out.println(steps);

Getting the steps node and adding it to a new JSON:

ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(new File("/path/to/your/json/file"));

JsonNode steps = root.get("fgjf").get(0).get("steps");

ObjectNode newRoot = mapper.createObjectNode();
newRoot.set("steps", steps);
System.out.println(newRoot);
cassiomolin
  • 124,154
  • 35
  • 280
  • 359