0

I can't figure out how I would do the ff.

I have the ff. Payload

   {
    "code": null,
    "message": null,
    "recCtrlOut": {
    },
    "acctSumm": [
        {
            "acctBasic": {
                "acctType": "SV",
                "acctId": "123",
                },
            "acctBasic": {
                "acctType": "SV",
                "acctId": "321",
                }
}
]
        
}

And I just want to get the acctId params and assign it to a new plain array of accountIds. How do I do it in Spring/Java?. Thanks

2
  • Your JSON is invalid Commented Oct 5, 2021 at 17:07
  • ok, i updated it , didnt noticed i missed the square brackets when redacting the payload Commented Oct 6, 2021 at 2:07

1 Answer 1

2

Try using json path. Library can be found here. E.g. say you had json like this:

{
  "code": null,
  "message": null,
  "recCtrlOut": {
  },
  "acctSumm": [
    {
      "acctBasic": {
        "acctType": "SV",
        "acctId": "123"
      }
    },
    {
      "acctBasic": {
        "acctType": "SV",
        "acctId": "321"
      }
    }
  ]
}

Actual code would be something like:

List<String> ids = JsonPath.read(json, "$.acctSumm[*].acctBasic.acctId");

The above list will now hold:

["123","321"]

If you wanna learn json path syntax, you could try using this online tool. Here is also a guide to help you get started with json path.

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

Comments

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.