2

I'm getting json object on my mongodb with virustotal API This is how a json object stored in mongodb object looks like :

{
"_id" : ObjectId("597cd2f871eac714388b2f7f"),
"results" : {
    "scans" : {
        "Bkav" : {
            "detected" : true,
            "version" : "1.3.0.8042",
            "result" : "W32.HfsAutoB.971A",
            "update" : "20160706"
        },
        "TotalDefense" : {
            "detected" : false,
            "version" : "37.1.62.1",
            "result" : null,
            "update" : "20160706"
        },
        "MicroWorld-eScan" : {
            "detected" : true,
            "version" : "12.0.250.0",
            "result" : "Packer.Expressor.B",
            "update" : "20160706"
        },
        "nProtect" : {
            "detected" : true,
            "version" : "2016-07-06.01",
            "result" : "Packer.Expressor.B",
            "update" : "20160706"
        },
        "ALYac" : {
            "detected" : false,
            "version" : "1.0.1.9",
            "result" : null,
            "update" : "20160706"
        },           
        "TrendMicro" : {
            "detected" : true,
            "version" : "9.740.0.1012",
            "result" : "TROJ_GEN.R047C0CAP16",
            "update" : "20160706"
        },
        "McAfee-GW-Edition" : {
            "detected" : true,
            "version" : "v2015",
            "result" : "BehavesLike.Win32.Flyagent.cc",
            "update" : "20160706"
        },
        "Sophos" : {
            "detected" : true,
            "version" : "4.98.0",
            "result" : "W32/Pidgeon-A",
            "update" : "20160706"
        },
        "Cyren" : {
            "detected" : true,
            "version" : "5.4.16.7",
            "result" : "W32/SysVenFak.A.gen!Eldorado",
            "update" : "20160706"
        },
        "Microsoft" : {
            "detected" : true,
            "version" : "1.1.12902.0",
            "result" : "Backdoor:Win32/Delf.SJ",
            "update" : "20160706"
        },
        "AegisLab" : {
            "detected" : true,
            "version" : "4.2",
            "result" : "Backdoor.W32.BlackHole.acx!c",
            "update" : "20160706"
        },            
        "Qihoo-360" : {
            "detected" : false,
            "version" : "1.0.0.1120",
            "result" : null,
            "update" : "20160706"
        }
    },
    "scan_id" : "2ad6e0aad0b40f152f234787daa4afb87538f3278f5c8f815d53ef46d5eea4ac-1467833095",
    "sha1" : "c5dcd5526ac5330ad1e9fad51488718329fdb697",
    "resource" : "0a60424e0967b6cfc172dac82e10a2fe",
    "response_code" : 1,
    "scan_date" : "2016-07-06 19:24:55",
    "permalink" : "https://www.virustotal.com/file/2ad6e0aad0b40f152f234787daa4afb87538f3278f5c8f815d53ef46d5eea4ac/analysis/1467833095/",
    "verbose_msg" : "Scan finished, information embedded",
    "total" : 54,
    "positives" : 41,
    "sha256" : "2ad6e0aad0b40f152f234787daa4afb87538f3278f5c8f815d53ef46d5eea4ac",
    "md5" : "0a60424e0967b6cfc172dac82e10a2fe"
},
"response_code" : 200
}

As you can see the json object is too complicated to just get given value from, This is what i've tried so far :

MongoClient mongo = new MongoClient("localhost", 27017);
        MongoDatabase database1 = mongo.getDatabase(db);
        MongoCollection<Document> collection1 = database1.getCollection(col);
        try (MongoCursor<Document> cursor = collection1.find().iterator()){

            while (cursor.hasNext()){
                Document doc = cursor.next();
                List list = new ArrayList(doc.values());
                System.out.println(list.get(1));
            }

        }

I was thinking maybe there is a way to map all this json to a java class, the main problem is with the "scans" as there are many different scannors and it isn't optimized to create a java class model to each of them, My question is how can i store directly my json objects to a java object so as to operate on the results returned.

2 Answers 2

1

I am calling your main model as Scan. You can create a POJO (lets call it Scanner) with below attributes:
scannerName, detected, version, result, update;

Scanner.java

private String scannerName;
private String detected;
private String version;
private String result;
private String update;

Scan.java

private String scan_id;
private List<Scanner> = new ArrayList<Scanner>();
private String sha1;
private String resource;
......
........

So your scan model now has a List of scanners.

Using morphia its something like:

@Embedded
private List<Scanner> scanner;

If you are not using any wrapper around the java-driver, just try

private List<BasicDBObject>
Sign up to request clarification or add additional context in comments.

1 Comment

i'm a little bit confuse with what you are suggesting, as i said there a lot of scanners, how is it possible to store each of them in a list, can you please provide an example, I can't seem to understand your answer, thank you
1

You need to create two java classes in order to parse Json of coming from Mongo,Suppose First Class Name "CollectionReceived" Second Class Name "Result" In CollectionReceived class you need to declare members

public String _id;
public String response_code;
public Result result;

In Result class you need to declare members

Map<String,Map<String,Map<String,Map<String,Object>>>> results=new HashMap<String,Map<String,Map<String,Map<String,Object>>>>();,
public String scan_id;
public String sha1;
public String resource;
public String response_code;
public String scan_date;
public String permalink;
public String verbose_msg;
public String total;
public String positives;
public String sha256;
public String md5;

and don't forget to use @RestController on your controller, You will parse Json Easily and get all the values you required.

5 Comments

I don't understand very well what you are trying to say, and why would I add @RestController
Suppose i want to get the version of BKav, how can i get that?
Bkav is the HashMap and you can get its value based on its key and in your case key is version
@RestController will parse your Json in to java object.
This is the annotation used in Spring boot if i'm not mistaken, i'm not working with that

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.