0

I have a JSON array as you can see below with multiple JSON objects having two keys:

1) submitted_datetime

2) value

I need to remove those JSON objects whose value is repeated and keep only the most recent value.

I want to keep duplicated values, but not repeat the same value in a row.

I have to do this with Java language!!!

Any ideas ? Thanks in advance

Sample JSON

[{
        "submitted_datetime": "2018-06-29 11:14:30",
        "value": 6
    }, {
        "submitted_datetime": "2018-06-29 11:16:20",
        "value": 6
    }, {
        "submitted_datetime": "2018-06-29 11:41:59",
        "value": 6
    }, {
        "submitted_datetime": "2018-06-29 11:43:49",
        "value": 2
    }, {
        "submitted_datetime": "2018-06-29 11:46:13",
        "value": 10
    }, {
        "submitted_datetime": "2018-06-29 11:46:42",
        "value": 10
    }, {
        "submitted_datetime": "2018-06-29 12:01:33",
        "value": 8
    }, {
        "submitted_datetime": "2018-06-29 12:02:28",
        "value": 8
    }, {
        "submitted_datetime": "2018-06-29 12:35:52",
        "value": 6
    }, {
        "submitted_datetime": "2018-06-29 12:35:53",
        "value": 8
    }
]

Expected Result

[   {
        "submitted_datetime": "2018-06-29 11:41:59",
        "value": 6
    }, {
        "submitted_datetime": "2018-06-29 11:43:49",
        "value": 2
    }, {
        "submitted_datetime": "2018-06-29 11:46:42",
        "value": 10
    }, {
        "submitted_datetime": "2018-06-29 12:01:33",
        "value": 8
    }, {
        "submitted_datetime": "2018-06-29 12:35:52",
        "value": 6
    }, {
        "submitted_datetime": "2018-06-29 12:35:53",
        "value": 8
    }
]
2
  • Have you tried anything so far? Commented Jun 29, 2018 at 10:36
  • Why dont you use a hashmap before you convert it to json? (assuming that the data is not originally a json) Commented Jun 29, 2018 at 10:37

1 Answer 1

1

If you are willing to use Jackson, a popular JSON parser for Java, you could create a class to map the items to:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {

    @JsonProperty("submitted_datetime")
    private String submittedDateTime;

    private Integer value;

    // Getters and setters
}

Then read the JSON array as a list:

ObjectMapper mapper = new ObjectMapper();
List<Foo> list = mapper.readValue(json, new TypeReference<List<Foo>>() {});

Then build a new list with the items you need (I'm assuming the list is ordered):

Foo previousItem = null;
List<Foo> filteredList = new ArrayList<>();

for (Iterator<Foo> iterator = list.iterator(); iterator.hasNext();) {

    Foo item = iterator.next();

    if (previousItem != null && !item.getValue().equals(previousItem.getValue())) {
        filteredList.add(previousItem);
    }

    if (!iterator.hasNext() && !item.getValue().equals(previousItem.getValue())) {
        filteredList.add(item);
    }

    previousItem = item;            
}

And finally create a new JSON:

String newJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(filteredList);

The output will be:

[ {
  "value" : 6,
  "submitted_datetime" : "2018-06-29 11:41:59"
}, {
  "value" : 2,
  "submitted_datetime" : "2018-06-29 11:43:49"
}, {
  "value" : 10,
  "submitted_datetime" : "2018-06-29 11:46:42"
}, {
  "value" : 8,
  "submitted_datetime" : "2018-06-29 12:02:28"
}, {
  "value" : 6,
  "submitted_datetime" : "2018-06-29 12:35:52"
}, {
  "value" : 8,
  "submitted_datetime" : "2018-06-29 12:35:53"
} ]
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.