Sunday, 24 April 2016

How to iterate JSON file from child node (tags.Component) and finding their parent nodes (resources name & attributes) ???

package com.codingmania.Resource;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.MissingNode;

import com.codingmania.model.Component;
import com.codingmania.model.Resource;

public class MainResource
{
public static void main(String[] args) throws JsonProcessingException, IOException
{

JsonNode root,moduleNode,resourceNode,instanceNode,primaryNode,attributesNode,jsonValue=null;
ObjectMapper mapper = new ObjectMapper();
List<JsonNode> list = new ArrayList<JsonNode>();

root = mapper.readTree(new File("src/main/resources/terraform.json"));

Map<String, Component> coMap = new HashMap<String, Component>();

moduleNode = root.path("modules");

   System.out.println("-------tag.Component parsing start-------");
 
   for (JsonNode node : moduleNode)
    {
    resourceNode = node.get("resources");
 
    Iterator<String> itr = resourceNode.getFieldNames();
    while (itr.hasNext())
    {
    String resourceName = itr.next();
    instanceNode = resourceNode.path(resourceName);
   
    primaryNode = instanceNode.path("primary");
        System.out.println(primaryNode);

         attributesNode = primaryNode.path("attributes");
       
         jsonValue = attributesNode.path("tags.Component");
 if(jsonValue instanceof MissingNode)
 {
 list.remove(jsonValue);
 }else{
 String componentName = jsonValue.toString();
 componentName = componentName.replace("\"", "");
 Component component = coMap.get(componentName);
 if(component==null){
 component = new Component();
 component.setName(componentName);
 coMap.put(componentName, component);
 }

 Resource r  = new Resource();
 component.addResource(r);
 r.setName(resourceName);
 r.setType(instanceNode.get("type").getTextValue());
 r.setId(primaryNode.get("id").getTextValue());
 Iterator<String> fieldNames = attributesNode.getFieldNames();
 while (fieldNames.hasNext()) {
 String key = fieldNames.next();
 String value = attributesNode.get(key).getTextValue();
 r.addAttribute(key, value);
 }

}
 }

       
    }  

    System.out.println(mapper.writeValueAsString(coMap));
    }  
}
========================= Component.java  =============================
package com.codingmania.model;

import java.util.ArrayList;
import java.util.List;

public class Component {
String name;
List<Resource> resources;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public List<Resource> getResources() {
return resources;
}

public void setResources(List<Resource> resources) {
this.resources = resources;
}

public void addResource(Resource r) {
if(resources == null){
resources = new ArrayList<>();
}
resources.add(r);

}
}
=======================  Resource.java  =================================
package com.codingmania.model;

import java.util.HashMap;
import java.util.Map;

public class Resource {
String name;
String type;
String id;
Map<String, String> attributes;


public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Map<String, String> getAttributes() {
return attributes;
}


public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}

public void addAttribute(String key, String value) {
if(attributes == null){
attributes =new HashMap<>();
}
attributes.put(key, value);
}
}
-----------------------------------  terraform.json  -----------------------------------------------

For further requirements, please contact me on 8237976501 due to security reasons.

No comments:

Post a Comment

JSP interview questions and answers

Q1. What is JSP and why do we need it? JSP stands for JavaServer Pages. JSP is java server side technology to create dynamic web pages. J...