0

Right now I'm trying to parse incoming JSON that is in this format:

{
  <email>: {
    <name>: <string>,     # setting value
     ...
  },
  ...
}

For example:

{
 "aaa@example.com": {
   "statement": true
 },
 "bbb@example.com": {
   "statement": false
 }
}

I also will not know how many emails will be in this JSON. I am a little befuddled as to how you could get all these emails with Jackson without knowing the property name for this, and I was wondering if it was possible.

Here is my code so far:

public class GDPRConsent extends Model {
    @JsonIgnore
    private static final String GDPR_CONSENT = "gdprConsent";

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @Override
    public String getId() {
        return GDPR_CONSENT;
    }
}

This question is not the same as the duplicate. I am looking to solve this without a key, I just have the emails not a key for all the emails.

Josh
  • 386
  • 5
  • 14
  • That duplicate did not help me – Josh Jul 19 '18 at 21:36
  • Follow the duplicated link - ultimately you need to create a Map of key/value (i.e., String, String) values. Then you'll have to walk through the map to determine what is there. – stdunbar Jul 19 '18 at 22:13
  • That's not the problem any longer, the problem is I don't have a key for the emails so I have nothing to grab. HashMap o = mapper.readValue(from, typeRef); <--- This line in the answers on the second duplicate would not work for me because my class GDPRConsent doesn't have any key to grab emails with. I don't understand how I would "walkthrough" the JSON without knowing anything about the property itself or how many values are there. – Josh Jul 19 '18 at 22:24
  • In that case take a look at [this post](https://stackoverflow.com/questions/26255089/how-to-get-string-from-jsonobject-without-specific-name/26255338) – stdunbar Jul 19 '18 at 22:52
  • Doesn't work either because it doesn't account for nested JSON – Josh Jul 19 '18 at 23:06

0 Answers0