Search code examples
scalajacksonjackson-databind

jackson read node field with $ symbol


response from API looks like

  "node1": [
       {
           "$field": "https://somethingh.com",
           "type": "object",
           "meta:Type": "object"
       },
       {
           "$field": "https://somethingelse.com",
           "type": "object",
           "meta:Type": "object"
       }
   ],

When retrieving the node by key , some times it is retrieving , some time objNode.get("$field") returns null. so getting null pointer exception.

What are the ways i can avoid it . I dont want to use get(0) ( the index of the element) . because latter thee position may change..

    val mapper = new ObjectMapper()
    val node = mapper.readTree(res)
    val nodelist = node.get("node1")

    if (nodelist != null) {
     
      for (objNode <- nodelist) {
        println(s"objNode => $objNode  and  value => ${objNode.get("$field")} ")
        val fieldColumn = objNode.get("$field").textValue()
}

Solution

  • The issue was the $ which was a special character . The code worked as is.