0

After some mapping that ́s out of my control, I receive and Object that looks like this: enter image description here

This is how I ́m getting this Object, from the original message

Object valueArray = x.getFieldData().getData().getAdditionalProperties().get("items");

I need to get the value out of each element, do some basic math operations, and update the original message. If it ́s not an array I don ́t really have a problem, but I can ́t seem to in this case. This is how I ́m doing it for non arrays:

Object value = x.getFieldData().getData().getAdditionalProperties().get("value");
 if(value != null){
 Double result = (Double) value * Double.parseDouble(rule.values.get(0));
 x.getFieldData().getData().getAdditionalProperties().put("value", result);
 }

Any ideas on how to approach this? Thank you

asked Jul 1, 2021 at 8:44
3
  • Cast that valueArray to an ArrayList and iterate through it... Commented Jul 1, 2021 at 8:46
  • I'd rather check for valueArray instanceof List or maybe even valueArray instance of Iterable and cast accordingly. However, it's still not clear to me what level of the hierarchy you are struggling with. Is it the list containting the nested map? Or is it the "labels" list inside the map? Commented Jul 1, 2021 at 8:49
  • Thanks for the comments. I just don´t know how to go about getting the specific 'value' and then back into the original object. If I cast it into an ArrayList I can get all the information, but not specificially the 'value' Commented Jul 1, 2021 at 9:02

1 Answer 1

1

It kind of depends on how your data is documented. You have to typecast everything staying safe, and for that you need to make some assumptions:

if (valueArray instanceof List) {
 @SuppressWarnings("unchecked") // <- need a type in List declaration rather than '?' to allow for List.set, 'Object' being safe
 List<Object> valueList = (List<Object>) valueArray;
 for (int i = 0; i < valueList.size(); i++) {
 Object val = valueList.get(i);
 if (val instanceof Map) {
 @SuppressWarnings("unchecked") // <- you need to be sure that all keys are strings to do that, otherwise use <Object, Object> and typecast again as needed
 Map<String, Object> mVal = (Map<String, Object>) val;
 //Do something with you map, e.g. change some value:
 mVal.put("myprop", "a-value");
 }
 //Or change the value entirely, e.g.:
 valueList.set(i, new LinkedHashMap<String, Object>());
 }
}

Note that instanceof checks also check for null.

Now if you're 100% guaranteed that the read object is always a java.util.List (as ArrayList is) of java.util.Map (LinkedHashMap is a subclass of Map) with keys of type String, you can simplify to this:

if (valueArray instanceof List) {
 @SuppressWarnings("unchecked") // <- need to be 100% sure of all types involved every time: UNSAFE operation!
 List<Map<String, Object>> valueList = (List<Map<String, Object>>) valueArray;
 for (int i = 0; i < valueList.size(); i++) {
 Map<String, Object> val = valueList.get(i);
 if (val != null) {
 //Do something with you map, e.g. change some value:
 val.put("myprop", "a-value");
 }
 //Or change the value entirely, e.g.:
 valueList.set(i, new LinkedHashMap<String, Object>());
 }
}
answered Jul 1, 2021 at 9:20
1
  • Thanks a lot, this is what I was looking for! On the long run I probably will ask the other team to send the data better structured, but for now this works. Commented Jul 1, 2021 at 11:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.