Add a property to a json string with jackson json

17,402

Try casting your JsonNode to an com.fasterxml.jackson.databind.node.ObjectNode and then calling put set (or replace) on it.

Share:
17,402

Related videos on Youtube

Dimitri Kopriwa
Author by

Dimitri Kopriwa

Advanced expertise in Internet technologies. Fully familiar with the DevOps movement, we do apply Agile development methodologies proven to create state-of-the-art software solutions, designed to scale globally. As R&D experts in Web technologies, we bring digital innovation added value to your business. If strengthening your information systems and developing your business through continuous improvements is your growth path, then our visions converge. Previously, at Moma Group R & D on the Voltalis energy saving project our aim was to promote responsible and sustainable use of electrical energy. In addition, we have also created technologies for cleaner energy.

Updated on July 22, 2022

Comments

  • Dimitri Kopriwa
    Dimitri Kopriwa almost 2 years

    I am storing a json string into a text field in mysql. After the insertion, i want to update my json string and add the mysql line id into it with jackson json.

    I have a java String which is in Json format

    {
      "thing":"val"
    }
    

    I'm looking to add another K/V without writing lines of codes.

    to finally have this :

    {
      "thing":"val"
      "mysqlId":10
    }
    

    I can convert my String to a JsonNode :

    ObjectMapper mapper = new ObjectMapper();
    JsonNode json = mapper.readTree( jsonStr);
    

    Looking to do something like this

    json.put("mysqlId",10);
    json.toString();
    

    then update in my text field with new json string in mysql

    I can't make it. I don't want use many class is there a simple way to do so with jackson?

  • Jay Q.
    Jay Q. almost 9 years
    put method in ObjectNode is deprecated in v2.4. You need to use set or replace.
  • Don Cheadle
    Don Cheadle over 8 years
    is there a way to leverage Jackson to just change the name of a property in a JSON string? I.e. suppose I want to change 'id' : 42 to 'my_id' : 42 - without having to know the value 42 and re-creating the property?