How to handle a JSON with fasterXML in java?

Asked

Viewed 76 times

0

I have a json that comes from the bank Mongodb, I have to take and manipulate the json using fasterXml. I will have to take the names of the json properties and separate them with dot(.) instead of comma(,)

Kind of :

"type" : "object",
  "id" : "urn:jsonschema:com",
  "properties" : {
    "id" : {
      "type" : "any"
    },
    "name" : {
      "type" : "string"
    },
    "version" : {
      "type" : "string"
    },
    "code" : {
      "type" : "string"
    },
    "clazz" : {
      "type" : "string"
    }
  }
}

And show like this :

properties{id.name.version.code.clazz};

Show everything you have inside the parent that is "properties", or something like that!!

  • You need to know all daughter properties of properties, is that it? What have you tried so far? Give more details about your need/problem.

  • I managed to put the solution here

1 answer

0


In mine I am using log to show instead of sysout

ObjectMapper mapper = new ObjectMapper();

List<String> node = new ArrayList<>();          
JsonNode objectRoot = mapper.readTree(code);
Iterator<String> fields = objectRoot.fieldNames();
String field = "";
while(fields.hasNext()) {
    field = fields.next();

    node.add(field);
}
LOGGER.info("GRAU I: "+ field);

JsonNode arrayRoot = mapper.readTree(code);

Iterator<JsonNode> elements = arrayRoot.elements();

JsonNode element;

while(elements.hasNext()) {
    element = elements.next(); 
    field = "";
    fields = element.fieldNames();

    while(fields.hasNext()){
        field = fields.next();

        node.add(field);

        LOGGER.info("GRAU II em diante: "+ field);
    }

    return node;

}

I guess that’s it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.