Spring Boot + Java : Keyword Based Search from JSON data

Viewed 938

I have a project in spring boot using java. I am using Spring boot 2.1.4. I implemented third party devices and call their APIs by using REST. I stored response in elastic search. I am using elastic search 7.3.
I have one API which fetch data from elastic search and front end will call this API and render data which is basically JSON data from third party API. Now I want to build one API that can return search result. For example somebody typing ip in search box then I have to find ip from JSON data not from elastic search and return result to front end.
I know that how to search data from elastic but I already fetched data and it rendered. It's another API to search data, I want to search data from rendered data. So it's a basically keyword search from JSON data by using java. I researched a lot but couldn't find anything relatable.

    {
      "data": {
        "attributes": {
          "last_analysis_results": {
            "AlienVault": {
              "category": "harmless",
              "engine_name": "AlienVault",
              "method": "blacklist",
              "result": "clean"
            },
            "BADWARE.INFO": {
              "category": "harmless",
              "engine_name": "BADWARE.INFO",
              "method": "blacklist",
              "result": "clean"
            },
            "CINS Army": {
              "category": "harmless",
              "engine_name": "CINS Army",
              "method": "blacklist",
              "result": "clean"
            },
            "CLEAN MX": {
              "category": "harmless",
              "engine_name": "CLEAN MX",
              "method": "blacklist",
              "result": "clean"
            },
            "Cisco Talos IP Blacklist": {
              "category": "harmless",
              "engine_name": "Cisco Talos IP Blacklist",
              "method": "blacklist",
              "result": "clean"
            },
            "DNS8": {
              "category": "harmless",
              "engine_name": "DNS8",
              "method": "blacklist",
              "result": "clean"
            },
            "ESTsecurity-Threat Inside": {
              "category": "harmless",
              "engine_name": "ESTsecurity-Threat Inside",
              "method": "blacklist",
              "result": "clean"
            },
            "Fortinet": {
              "category": "harmless",
              "engine_name": "Fortinet",
              "method": "blacklist",
              "result": "clean"
            },
            "GreenSnow": {
              "category": "harmless",
              "engine_name": "GreenSnow",
              "method": "blacklist",
              "result": "clean"
            },
            "IPsum": {
              "category": "harmless",
              "engine_name": "IPsum",
              "method": "blacklist",
              "result": "clean"
            },
            "Kaspersky": {
              "category": "harmless",
              "engine_name": "Kaspersky",
              "method": "blacklist",
              "result": "clean"
            },
            "Nucleon": {
              "category": "harmless",
              "engine_name": "Nucleon",
              "method": "blacklist",
              "result": "clean"
            },
            "OpenPhish": {
              "category": "harmless",
              "engine_name": "OpenPhish",
              "method": "blacklist",
              "result": "clean"
            },
            "Phishtank": {
              "category": "harmless",
              "engine_name": "Phishtank",
              "method": "blacklist",
              "result": "clean"
            },
            "Quick Heal": {
              "category": "harmless",
              "engine_name": "Quick Heal",
              "method": "blacklist",
              "result": "clean"
            },
            "Sophos": {
              "category": "harmless",
              "engine_name": "Sophos",
              "method": "blacklist",
              "result": "clean"
            },
            "Tencent": {
              "category": "harmless",
              "engine_name": "Tencent",
              "method": "blacklist",
              "result": "clean"
            },
            "URLhaus": {
              "category": "harmless",
              "engine_name": "URLhaus",
              "method": "blacklist",
              "result": "clean"
            }
          },
          "last_analysis_stats": {
            "harmless": 75,
            "malicious": 0,
            "suspicious": 0,
            "timeout": 0,
            "undetected": 0
          },
          "last_modification_date": 1587640540,
          "reputation": 0,
          "tags": [
            "private"
          ],
          "total_votes": {
            "harmless": 0,
            "malicious": 0
          },
          "whois": "NetRange: 10.0.0.0 - 10.255.255.255\nCIDR: 10.0.0.0/8\nNetName: PRIVATE-ADDRESS-ABLK-RFC1918-IANA-RESERVED\nNetHandle: NET-10-0-0-0-1\nParent: ()\nNetType: IANA Special Use\nOriginAS: \nOrganization: Internet Assigned Numbers Authority (IANA)\nRegDate: \nUpdated: 2013-08-30\nComment: These addresses are in use by many millions of independently operated networks, which might be as small as a single computer connected to a home gateway, and are automatically configured in hundreds of millions of devices. They are only intended for use within a private context and traffic that needs to cross the Internet will need to use a different, unique address.\nComment: \nComment: These addresses can be used by anyone without any need to coordinate with IANA or an Internet registry. The traffic from these addresses does not come from ICANN or IANA. We are not the source of activity you may see on logs or in e-mail records. Please refer to http://www.iana.org/abuse/answers\nComment: \nComment: These addresses were assigned by the IETF, the organization that develops Internet protocols, in the Best Current Practice document, RFC 1918 which can be found at:\nComment: http://datatracker.ietf.org/doc/rfc1918\nRef: https://rdap.arin.net/registry/ip/10.0.0.0\nOrgName: Internet Assigned Numbers Authority\nOrgId: IANA\nAddress: 12025 Waterfront Drive\nAddress: Suite 300\nCity: Los Angeles\nStateProv: CA\nPostalCode: 90292\nCountry: US\nRegDate: \nUpdated: 2012-08-31\nRef: https://rdap.arin.net/registry/entity/IANA\nOrgAbuseHandle: IANA-IP-ARIN\nOrgAbuseName: ICANN\nOrgAbusePhone: +1-310-301-5820 \nOrgAbuseEmail: abuse@iana.org\nOrgAbuseRef: https://rdap.arin.net/registry/entity/IANA-IP-ARIN\nOrgTechHandle: IANA-IP-ARIN\nOrgTechName: ICANN\nOrgTechPhone: +1-310-301-5820 \nOrgTechEmail: abuse@iana.org\nOrgTechRef: https://rdap.arin.net/registry/entity/IANA-IP-ARIN\n",
          "whois_date": 1585897644
        },
        "id": "10.2.2.2",
        "links": {
          "self": "https://www.virustotal.com/api/v3/ip_addresses/10.2.2.2"
        },
        "type": "ip_address"
      }
    }

This is sample JSON data and every time structure would be different also keys for each object would be different.
My Spring boot API must have two arguments first is JSON data and second is searched text. So it should be look like

    @PostMapping(value = "/search")
    public Object keywordSearch(@RequestBody JSONObject object, @RequestParam String s) throws Exception 
    {
                String string = object.toJSONString();
                // next operation
    }

It must return searched text from JSON data for giving result to front end.
I don't understand what to do next. Please help me any help and suggestions will be appreciated.

1 Answers

Amith Walker in Above code occure NullpointerException so , Ignore the Key that value is Null like , "value" : null

and I also develop code on it

// ListMap For Store the Searchable Data.
List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();

 public List<Map<String, Object>> getKeyAndValues(String jsonString, String s) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject object;
        try {
            object = (JSONObject) new JSONParser().parse(jsonString);
            JSONObject obj2 = new JSONObject();
            JSONObject obj3 = new JSONObject();
            Iterator<?> setOfObject = object.keySet().iterator();
            while (setOfObject.hasNext()) {
                String key = (String) setOfObject.next();
                if (object.get(key) instanceof JSONObject) {
                    obj2 = (JSONObject) object.get(key);
                    getKeyAndValues(obj2.toJSONString(), s);
                }
                if (object.get(key) instanceof JSONArray) {
                    JSONArray array = (JSONArray) object.get(key);
                    Iterator<?> setOfArray = array.iterator();
                    while (setOfArray.hasNext()) {
                        obj3 = (JSONObject) setOfArray.next();
                        getKeyAndValues(obj3.toJSONString(), s);
                    }
                } else {
                    if (object.get(key) != null) {
                        if (key.contains(s)) {
                            map.put(key, object.get(key));
                        }
                        if (object.get(key).toString().contains(s)) {
                            map.put(key, object.get(key));
                        }
                    }
                }
            }
             // Only Non-Empty Map add into ListMap..
            if (!map.isEmpty()) {
                listMap.add(map);
            }
        } catch (ParseException e) {
            
        }
        return listMap;
    }
Related