0

I have a rather large JSON object (this is a subset) I'm trying to parse:

{
  "items": [
    {
      "Name": "Wallet",
      "tags": [
        "wallet",
        "cardholder"
      ],
      "features": {
        "material": {
          "location": "in-house"
        },
        "stitching": {
          "location": "in-house"
        }
      },
      "color": null,
      "store": {
          "address": "123 Main Street"
      }
    }
  ],
  "jItem": 0
}

I have Java POJO's for all JSON objects except for the features object, which contains objects where the key is a dynamic value. My current only-POJO's code does this:

...
itemsJson = doGet(url);
ObjectMapper objMapper = new ObjectMapper();
Items items = objMapper.readValue(itemsJson, Items.class);
...

This gives me a hierarchy of Java POJO's representing my data. The one hitch is features. How can I parse the features data, with the keys as values, within this larger object? I've looked at other SO posts:

  1. Deserialize JSON in Jackson where key is a value
  2. Deserializing jackson dynamic key value
  3. Jackson JSON key as value in Java

but none of these solutions have 1. an object where the key is the value and 2. an object where the key is the value is contained within an object. I do have parsing just the features working using this "just features JSON":

{
  "features": {
    "material": {
      "location": "in-house"
     },
     "stitching": {
       "location": "in-house"
      }
  }
}

with this code:

...
JsonNode jsonNodeRecord = objectMapper.readTree(App.class.getResourceAsStream("/data.json"));
List<JsonNode> recordNodes = jsonNodeRecord.findValues("features");
...

which gives me JsonNode's. This isn't ideal because I don't have my features data in a POJO.

The Question: It's not clear to me how to integrate parsing the JSON using POJO's for everything except for features, with either the JsonNode code above or a custom deserializer as in the #1 SO link above.

Nho Jotom
  • 391
  • 1
  • 3
  • 7
  • I'm not sure I understand the target model. What's the "key in the larger object" you're talking about? – daniu May 18 '21 at 17:57
  • @daniu If the features object were removed from the items object that contains it, I could parse items easily using Jackson and POJO's. I can parse the features object by itself, as if it were all of the JSON data. The features object has keys as values **and** is contained within the items object. I'll try to clarify the comment. – Nho Jotom May 18 '21 at 18:14
  • from whatever I understood from the post. You're basically trying to parse features object which is basically a Map. This should not cause any problems. If by any chance you're seeing features as null after reading though object mapper, add "@JsonProperty" annnotation on the field in Item class. I hope this solves your issue. – mike May 18 '21 at 18:40
  • @mike Thanks for the suggestion. I tried adding @JsonProperty to the features field in the Item class and I'm getting an Exception: `com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.LinkedHashMap` from Array value (token `JsonToken.START_ARRAY`) at [Source: (StringReader); line: 905, column: 20] (through reference chain: Item["items"]->java.lang.Object[][7]->Items["features"])` Looks to me like Jackson thinks it's an array, but it's not. – Nho Jotom May 18 '21 at 22:34
  • Just to make sure no one spends any more time on this right now, it turns out the JSON being returned is an object in one record and array in another (hence Jackson thinking it's an array). I'll respond to using @JsonProperty when the JSON is fixed. – Nho Jotom May 18 '21 at 23:39
  • having an inconsistent response becomes tricky with object mapper – mike May 19 '21 at 16:20

0 Answers0