2

I am a noob in Java and am struggling to with type conversions. I have a JSON Object like below:

   [  
       {  
        "A":{  
           "B":{  
              "C":"Message",
              "D":"FN1"
           }
        }
       }
    ] 

And I want to convert it like:

  [  
       {  
        "A":{  
           "B": "My String Message"
        }
       }
    ]

I am able to replace the old JSON node with new JSON node but unable to replace it with TextNode, tried multiple failed options like below:

   JsonNode newNode = new TextNode("My String Message");
   ObjectNode nodeObj = (ObjectNode) jsonNode;
   nodeObj.removeAll();
   nodeObj.set(newNode);

1 Answer 1

4

You had just a small problem with your code. When you add the new text entry, you have to provide the key value that you want to associate the new text node with. So this line:

nodeObj.set(newNode);

Just needs to be this:

nodeObj.set("B", newNode);

Here's a complete example that does just what you show in your question, incorporating the code you provided, with just this one small fix:

public static void main(String... args) throws IOException {

    // Read in the structure provided from a text file
    FileReader f = new FileReader("/tmp/foox.json");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(f);

    // Print the starting structure
    System.out.println(rootNode);

    // Get the node we want to operate on
    ObjectNode jsonNode = (ObjectNode)rootNode.get(0).get("A");

    // The OPs code, with just the small change of adding the key value when adding the new String
    JsonNode newNode = new TextNode("My String Message");
    ObjectNode nodeObj = (ObjectNode) jsonNode;
    nodeObj.removeAll();
    nodeObj.set("B", newNode);

    // Print the resulting structure
    System.out.println(rootNode);
}

And the resulting output:

[{"A":{"B":{"C":"Message","D":"FN1"}}}]
[{"A":{"B":"My String Message"}}]

UPDATE: Per the discussion in the comments, to make the change you illustrate, you have to have access to the ObjectNode associated with the key A. It is this ObjectNode that associates B with the ObjectNode containing C and D. Since you want to change the value that B is associated with (you want it to be associated with a TextNode instead of an ObjectNode), you need access to the ObjectNode associated with A. If you only have a pointer to the ObjectNode containing C and D, you can't make the change you want to make.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the help but only problem here is that i do not know the parent of the node. In above example, B can be anything (X/Y/Z) with same children (C & D). And i am unable to get the parent for my 'jsonNode'
Oh, so you only have a pointer to whatever B's value is? - If that's true, you can't do what you want to do. B's value is a Map, and you want to turn that into a String. Since that means a new object, you have to manipulate the pointer to that object, and that pointer is in the Map value associated with the key 'A'. So you need a pointer to the Map associated with 'A'. There's no way around that that that I can see. That's where the value that you need to change lives.
What your code is attempting to do, if jsonNode points to B's value, is delete everything out of the Map B, and then put a string into that Map. But an entry in a Map requires a key. That's why my change was needed. If you only have a pointer to that Map, then you can't turn it into a String. You just can't. It's a Map. All you can do is remove things from the Map and add new things. The best you can do is [{"A":{"B":{"SomeKey":"My String Message"}}}]
I think your question isn't phrased quite right. I think you mean replace an ObjectNode with a ArrayNode of ObjectNodes, right? - If so, this is the same case as the other...you're just replacing an ObjectNode with an ArrayNode instead of a TextNode, right?
Check this out: pastebin.com/zAuDm1eK - replace your one-liner to create the new TextNode with this code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.