0

I'd like to keep only a some part of the JSON by using jsonpath.
Let's assume I have to the following JSON:

{  "loginId":"bootstrap",
   "isTechnicalUser":false,
   "name":{  "+
      "title":"Mr.",
      "firstName":"Boot",
      "familyName":"Strap""+
   },
}

I want a JSON containing only $.loginId and $.name.firstName.

How can I do that with jsonpath? Or achieve that with other framework?

Thanks!

////////////// UPDATE //////////////
Forgot to mention that the configuration has to be dynamic done by the user so I cannot hard code anything (i.e. using Transient or similar)

////////////// UPDATE 2 //////////////
Perhaps it could work that first delete the nodes with jsonpath and find the differences between this map and the original one. However I cannot find a deep map compare tool. I checked guava's but it is not good enough.

Viktor
  • 1,325
  • 2
  • 19
  • 41

1 Answers1

0

Use @Transient to mark the fields that you want to ignore while serialising the object.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Santosh b
  • 729
  • 1
  • 8
  • 19
  • 1
    Have you thought about the implications that might come along with the _transient_ keyword? Also JPA will evaluate that keyword and other frameworks might, too. – Jan B. Feb 25 '19 at 11:07
  • 1
    Matt you are right. Whatever i said makes sense only if the object is a UI/DTO Object and not persistent Object – Santosh b Feb 25 '19 at 11:09
  • Agreed. However, DTOs are typically made to be serialized. If you mark a field with _@Transient_ in a DTO, you probably should ask yourself why the property is present, anyway. But sure, it's hard to tell with that little bit of context given by the OP. – Jan B. Feb 25 '19 at 11:16
  • Thanks but the problem is that it has to be a dynamic configuration. I.e. the user can configure what they want to see. I know WriteContext has a method to delete node bay json path but I need the other way i.e. keep them. – Viktor Feb 25 '19 at 11:30