How to expand JSON structure in JAVA - java

I have a JSON structure, which is small.
My requirement is to expand the JSON structure.
Example JSON structure :
'{
"CallLog":{
"Three":{
"age":100,
"name":"Sample",
"Other":100,
"Add":"Sample"
},
"One":{
"CallLogEntry":[
{
"ContryCode":{
"CountryCode":123
}
},
{
"Phone Number":{
"PhoneNumber":456
}
},
{
"Name":{
"name":456
}
}
]
},
"Two":{
"age":100,
"name":"Sample",
"Other":100,
"Add":"Sample"
}
}
}
So, i want to expand his JSON.
Lets say i want repeat "One" node 10 times, and "two" node 5 times, and then write this expanded JSON in the new JSON.
How can i do this?

If you want to do it through java, i would recommend converting this JSON to java code first.Once you get the corresponding classes e.g. CallLog.java,CallLogEntry.java etc , you can define a new Class CallLog1.java and declare 10 'One' , 5 'Two' etc , as per your requirement and then create the JSON object. You can copy paste your json to http://www.jsonschema2pojo.org/ to get POJO , and then create you own POJO , and convert it into JSON , using library like GSon.

Related

Parse a JSON string in Java

I would like to know how I can access the data from the below JSON String.
{
"posters": {
"thumbnail": "http://google.com/images/11420914_ori.jpg",
"profile": "http://google.com/images/11420914_ori.jpg"
}
}
I want this to be done in Java Lists (NOT by using JSON Parsers)
Expected output:
thumbnail: "http://google.com/images/11420914_ori.jpg"
profile: "http://google.com/images/11420914_ori.jpg"
You need a Json Mapper, I recommend you to read this : Jackson in 5 minutes

Jersey rest server - Return different structure for list as json

I'm experiencing a different json structure of a returned list when running on Tomcat.
{
"apartment": [
{
"apartmentName": "ABC",
"id": "1"
},
{
"apartmentName": "DEF",
"id": "2"
}
]
}
Since the controller is returning List of apartments; ideally it should start with square braces [] instead of curly braces. This results into JSON parser on client side think that it's a JSONObject instead of JSONArray. I am not sure how to fix this. Below are the various POM dependency and their version.
Tomcat version I am using is 7.0.67
jersey-server-1.19
jersey-servlet-1.19
jersey-json-1.19
jersey-spring-1.19
jersey-core-1.19
This is because you're returning a List of Apartments from your servlet. You can fix this by returning an array like this
return new Apartment[]; // Collect all apartment objects and return it as an array
and Jackson will convert it accordingly. In your case I think you have Java object that has a parameter called apartments of type List. I assume you're using Jackson as the provider.

JSON to CSV conversion using Java

Is there any way to convert multiple JSON files into one CSV-file ?
My JSON file is like this:
{
"Title" : {
"name" : "ABC",
"id" : "1",
"job": "Teacher"
},
"Circle":{
"area":"2R"
},
"Triangle":{
"length":"45"
}
}
If you will see, this JSON has 3 roots and different elements under each root.
How to convert this JSON to CSV so that it can be opened in excel and can be viewed as follows:
Title
Name ABC
id 1
job Teacher
Circle
area 2r
Triangle
length 45
Can someone please suggest?
It would say that you should first parse you JSON with something like Jackson, and then you can write down a CSV file by using their extension. That's an option.
Other way might be to use an external tool to do the conversion, such as json2csv.
Hope it helps!
There is a library json2flat. It converted your json
{
"Title" : {
"name" : "ABC",
"id" : "1",
"job": "Teacher"
},
"Circle":{
"area":"2R"
},
"Triangle":{
"length":"45"
}
}
to the following
/Title/name,/Title/id,/Title/job,/Circle/area,/Triangle/length
"ABC","1","Teacher","2R","45"
Hope it helps. After all it depends upon users how they want to interpret it.

How to parse JSON data without key names into a Java class structure?

I successfully followed the example Simple Spring code to parse JSON into a Java class structure using Jackson.
Now i am looking for a hint how to do the same for JSON data without key names, e.g.
{
"10869918": {
"BRANCH": "Dienstleistungen",
"SECTOR": "Diverse"
},
"12254991": {
"BRANCH": "Luft- und Raumfahrtindustrie",
"SECTOR": "Logistik"
},
"12302743": {
"BRANCH": "Touristik und Freizeit",
"SECTOR": "Medien/Freizeit"
}
}
I doubt this is possible with a POJO-JSON mapper. You could use libraries like json-simple to parse the JSON string into Java objects (which basically are maps and lists) and access values like "10869918" by reading the keys of those maps.

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/

Categories

Resources