Search code examples
javaarraysjsonkatalon-studiojsonnode

How to add field names and values of a json object into List<String> using jsonNode


I have an expected json file as below. It is a nested object

{
    "contact_info": [{
        "contact_id": "Contact_001",
        "contact_status": {
            "exam_status": 0,
            "term_list": [{
                "term_code": 110,
                "t_list": [{
                        "s_code": "12001",
                        "sexam_status": 0
                    },
                    {
                        "s_code": "13001",
                        "sexam_status": 1
                    }
                ]
            }]
        }
    }]
}

How to add all field name and value of all items inside this object into List? I tried using the following way but it is not as expected.

//Get all key name in json object

```public static List<String> getAllKeysInJsonUsingJsonNodeFieldNames(String json) {
    ObjectMapper mapper = new ObjectMapper()
    List<String> keys = new ArrayList<>();
    JsonNode jsonNode = mapper.readTree(json);
    getAllKeysUsingJsonNodeFields(jsonNode, keys);
    return keys;
}

public static void getAllKeysUsingJsonNodeFields(JsonNode jsonNode, List<String> keys) {
    if (jsonNode.isObject()) {
        Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
        fields.forEachRemaining({def field ->
            keys.add(field.getKey());
            getAllKeysUsingJsonNodeFields((JsonNode) field.getValue(), keys);
        });
    } else if (jsonNode.isArray()) {
        ArrayNode arrayField = (ArrayNode) jsonNode;
        arrayField.forEach({def node ->
            getAllKeysUsingJsonNodeFields(node, keys);
        });
    }
}```

The result as below => It only shows only field name without figure out that field belong to which object

TagName[i] = s_code

I expect that the result should be below, specifically, I want to field name should be shown it belongs to which object => Could you tell me how to resolve it?

TagName[i] = contact_status.term_list[0].t_list[0].s_code

Sorry for I'm a new in Java => If anyone know a way to resolve it please tell me in more details. Thank you so much!


Solution

  • You can use JSON library such as Josson to do the transformation.

    https://github.com/octomix/josson

    Deserialization

    Josson josson = Josson.fromJsonString(
        "{" +
        "    \"contact_info\": [{" +
        "        \"contact_id\": \"Contact_001\"," +
        "        \"contact_status\": {" +
        "            \"exam_status\": 0," +
        "            \"term_list\": [{" +
        "                \"term_code\": 110," +
        "                \"t_list\": [{" +
        "                        \"s_code\": 12001," +
        "                        \"sexam_status\": 0" +
        "                    }," +
        "                    {" +
        "                        \"s_code\": 13001," +
        "                        \"sexam_status\": 1" +
        "                    }" +
        "                ]" +
        "            }]" +
        "        }" +
        "    }]" +
        "}");
    

    Query the keys and values in separate arrays

    Josson flattened = josson.getJosson("contact_info[0].flatten('.','[%d]')");
    
    System.out.println("-- Keys --");
    JsonNode node = flattened.getNode("keys()");
    List<String> keys = new ArrayList<>();
    node.elements().forEachRemaining(elem -> keys.add(elem.asText()));
    keys.forEach(System.out::println);
    
    System.out.println("-- Values --");
    node = flattened.getNode("toArray()");
    List<String> values = new ArrayList<>();
    node.elements().forEachRemaining(elem -> values.add(elem.toString()));
    values.forEach(System.out::println);
    

    Output

    -- Keys --
    contact_id
    contact_status.exam_status
    contact_status.term_list[0].term_code
    contact_status.term_list[0].t_list[0].s_code
    contact_status.term_list[0].t_list[0].sexam_status
    contact_status.term_list[0].t_list[1].s_code
    contact_status.term_list[0].t_list[1].sexam_status
    -- Values --
    "Contact_001"
    0
    110
    12001
    0
    13001
    1
    

    Query the keys and values in single array

    JsonNode node = josson.getNode("contact_info[0].flatten('.','[%d]').entries().concat(key,' = ',value)");
    List<String> keyValues = new ArrayList<>();
    node.elements().forEachRemaining(elem -> keyValues.add(elem.asText()));
    keyValues.forEach(System.out::println);
    

    Output

    contact_id = Contact_001
    contact_status.exam_status = 0
    contact_status.term_list[0].term_code = 110
    contact_status.term_list[0].t_list[0].s_code = 12001
    contact_status.term_list[0].t_list[0].sexam_status = 0
    contact_status.term_list[0].t_list[1].s_code = 13001
    contact_status.term_list[0].t_list[1].sexam_status = 1