Reading JSON data using Java - java

I have a 500,000 line json file I'm trying to read, I tried out a few online examples like mkyong's and many others, but this format is a first encounter for me. Can anyone help me to get started?
I'm using json-simple library.
Any enlighten on this topic? I'll be very grateful
{
"info": {
"category": "url",
"started": "2013-11-07 03:44:32",
"ended": "2013-11-07 03:46:55",
"version": "0.6",
"duration": 143,
"id": 37
},
"signatures": [],
"static": {},
"dropped": [
{
"yara": [],
"sha1": "6fd9eb6a42fd531988fba24137a2fe29ad992465",
"name": "tia[1].png",
"type": "PNG image data, 27 x 23, 8-bit/color RGB, non-interlaced",
"sha256": "75d6d26afb9506145d7f6472555855ef4305e0ef3e7cf4cb3d943c58123c7f74",
"crc32": "FDCAC92F",
"path": "/home/user/cuckoo/storage/analyses/37/files/2435944344/tia[1].png",
"ssdeep": null,
"size": 387,
"sha512": "b47ca17050ff4b6ddab848195c17b875454aafbec06d07bba126e553c9d32647f461adee9d1a75bbfffa08d6a8fc955429562794b123bebc9ec23dc89bdefcc5",
"md5": "ad07ee4cb98da073dda56ce7ceb88f5a"
}
]
}
REPLY
I currently don't understand how i can get the array in [dropped], I did some of the easier example, but this is a first for me. It seems like there a hierarchy but I don't know how to access it

Why not start with the json-simple decoding examples? If it's the size of the file you're worried about, then you should use the 'SAX' style example. This code would be a good example of how not to do it. See the other comment for better ideas.
JSONObject data= (JSONObject)JSONValue.parse(new InputStreamReader(JsonSimpleEx.class.getResourceAsStream("/test.json")) );
JSONArray dropped = (JSONArray)data.get("dropped");
JSONObject first = (JSONObject)dropped.get(0);
System.out.println( first.get("crc32"));

You can use Gson - from Google. It is simple and concise.
You need to declare your Java POJO that represents your JSON data, like:
public class Info {
private Category category;
...
}
Hope this help. Good luck.

Related

Extract subset of json from given JsonPath

I have a nested json structure and a list of json paths. I want to extract and return only those parts of json that are defined in the json paths.
For eg: Let's say the json contains:
{
"id": 1,
"user": {
"name": "ABCD",
"email": "abcd#email.com",
"phone": "1234"
}
}
And I've the following JsonPath's
$.id
$.user.name
$.user.phone
So the json returned will be
{
"id": 1,
"user": {
"name": "ABCD",
"phone": "1234"
}
}
I've tried multiple ways with Jayway's JsonPath but I only get the value, not the entire heirarchy(Eg: If I search for $.user.name then I get ABCD instead of {"user": {"name": "ABCD"}}).
I looked at Extracting a subset of attributes with JSONPath but it does not answer the question of return entire json structure.
Any ideas how do I approach this? I suspect that I would need to build Jackson JsonNode iteratively using the JsonPath, but couldn't build a solution.
Note: The example above contains a simple json structure. In reality, I suspect to have many nested structures(even arrays) inside json.

Extract and Crete new JSON string

I have some spring MVC based APIs which produces JSON/ XML which represents API output.
{
"data" :
{
"users": [
{
"id": "001",
"name: "abc1",
"type": {
"id": "P",
"name": "Permanent"
}
},
{
"id": "002",
"name: "xyz",
"type": {
"id": "C",
"name": "Contractor"
}
}
]
}
}
I'm passing a parameter with request as
url?fields=users.id, users.type.id
users.type.id is a sub-node in users node.
users node is an array.
Now, what I want to do is to filter those only properties and create the response based upon fields passed in the request.
So the response to above filter condition should be same structure and will only contain wanted fields with values.
I'm trying to build a flat map with keys with a dot notation so I won't lose the track to filter, then I'll rebuild the JSON again. I feel this approach is just unreasonable because Jackson has the .path and .with API's to check existing path. But the real challenge is to extract and create a new JSON which matches the response JSON.
I'm looking for some ideas to achieve this. I don't want to try any third party libs btw. I know some libs are there. I want to prefer Jackson way to do this.
Feel free to add or comment if you have a further ideas.

How to create a service using Google cloud endpoint which take a json value in input parameter

I wanted to create a service which can accept JSON as an input to my endpoint. eg.
[{ "name": "Ram","event": "processed" },
{"name": "Raj", "event": "click" }] .
Tried some of the possible ways, cloud endpoints are not supporting arrays or any array related types to use. I tried it to convert the json to string but it's not working for me.
Thanks in advance.
You seem to have an invalid JSON. Try validating it here. As you can see, you will encounter a parsing error.
Change all occurrences of “ with "
[
{
"name": "Ram",
"event": "processed"
},
{
"name": "Raj",
"event": "click"
}
]
and now you have a valid JSON.
More details on JSON can be found here.

Using JsonPath to extract value without knowing key

I have a json string like this:
"files": {
"fileA.c": {
"size": 100
},
"fileB.txt": {
"size": 200
}
}
I want to extract the file names, {"fileA.c","fileB.txt"}, using JsonPath. Note that the number of files is unknown.
The problem is, I don't know whether the file name is a key or a value:
If it is a key...well I certainly don't know the key name because that's the information I want to extract.
If it is a value, then what is its key?
Can I use JsonPath to extract the file names? If so, how?
If JsonPath cannot do this, is there any Java library for Json that can achieve this?
In your example, fileA.c and fileB.txt are keys, you can get them by iterating on the key in the enclosing object (which is referenced by the key "files").
I don't think JSONPath is really appropriate (or even applicable) in this case, it is designed to access elements when you know the structure of the documents, which means that basically you know the keys. It would be much easier to simply use a JSON parser.
I would suggest you to modify your data structure, to something like this;
var data = {
"files": [
{
"name": "fileA.c",
"size": 100
},
{
"name": "fileB.txt",
"size": 200
},
{
"name": "fileC.txt",
"size": 50
}
]
};
When structured like this, you can use DefiantJS (http://defiantjs.com) to query for files which sizes are larger than 100...like this:
JSON.search(data, '//*[size >= 100]/name')
DefiantJS extends the global object JSON with the method "search", with which you can query JSON structure with XPath expressions.
To see a working example with your data, check out this fiddle:
http://jsfiddle.net/jRN22/

Need help working with a json formatted string. (Java)

I have the contents of this page stored in a string variable. I've spent hours looking at different ways of working with json in java, and have learned a lot about the build path, dependencies, ssl certificates, and so on. I say that to emphasize the fact that I have been looking for an answer to this question independently. I've finally caved and I need help.
I'm looking for a way to turn this string into some kind of json array so that I could access the individual elements more easily. For example, something along the lines of:
int map = jsonArray[index].getInt("map_Id");
I hope that what I'm trying to do is clear enough. Any help would be appreciated.
edit: I'm currently attempting this with gson but am open to suggestions.
You can use the Jackson libraries used for JSON parsing as follows:
public static int getIntFromJson(String jsonString)
{
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonString);
int id = node.get(0).get("age").asInt();
return id;
}
Assuming that your jsonString will be something like this, the above code will return39.
[
{ "name": "Dagny Taggart", "age": 39 },
{ "name": "Francisco D'Anconia", "age": 40 },
{ "name": "Hank Rearden", "age": 46 }
]
You can pick a library (in this case, to decode JSON) from the Java section at the bottom of: http://json.org
YOu can have a look to the library below:
json-java
You can use the JSONObject to parse a String representing a JSON object and then extract the values you are interested in.

Categories

Resources