how to split 1GB JSON file into multiple JSON object file each - java

I have a JSON file of size 1GB which contains n number of twitter JSON Object tweets.Now I want to split large JSON file into single JSON Object each. But in large file the JSON chunks are space separated but not comma separated.
INPUT :-
{
"created_at": "Tue Aug 06 06:01:00 +0000 2013",
"id": 364627145785487360,
"id_str": "364627145785487360",
"text": "Soo sad right now..",
"filter_level": "medium",
"lang": "en"
}
{
"created_at": "Tue Aug 06 06:01:00 +0000 2013",
"id": 364627145785487360,
"id_str": "364627145785487361",
"text": "الضمير صوت هادىء يخبرك بأن احدا ينظر اليك",
"lang": "en"
}
Now my OUTPUT has to be as shown below
OUTPUT: Test1.json
{
"created_at": "Tue Aug 06 06:01:00 +0000 2013",
"id": 364627145785487360,
"id_str": "364627145785487360",
"text": "Soo sad right now..",
"filter_level": "medium",
"lang": "en"
}
Test2.json
{
"created_at": "Tue Aug 06 06:01:00 +0000 2013",
"id": 364627145785487360,
"id_str": "364627145785487361",
"text": "الضمير صوت هادىء يخبرك بأن احدا ينظر اليك",
"lang": "en"
}
Can anyone help me out to get the above output JSON files i.e, Test1.json, Test2.json

given number of lines (7000 in this case):
command : jq -c -M '.data[]' | split -l 7000

Related

can we parse a records with separators at multiple levels using bindy

we have got a requirement to convert a tab delimited data record into a JSON structure. But there are some delimiter inside each field as well.
Below would be the same data record
"123456 16 2014/6/960,2015/1/153 2014/2,2015/3"
below is the JSON we need to construct
[
{
"field1": "123456",
"field2": 16,
"node1": [
{
"year": 2014,
"qunatity": 6,
"discounts": 960
},
{
"year": 2015,
"qunatity": 1,
"discounts": 153
}
],
"node2": [
{
"year": 2014,
"qunatity": 2
},
{
"year": 2015,
"qunatity": 3
}
]
}
]
i am able to write a method in my flow that splits the fields and create a list of objects. But is there any way we can handle it without writing a custom methods

How to make a JSON object ordered?

Java file to set and get the values. I have set the boId as the first:
if(etsBuildOrder != null){
buildOrder.setBoId(etsBuildOrder.getBoId());
buildOrder.setName(etsBuildOrder.getName());
buildOrder.setFactory(etsBuildOrder.getFactory());
buildOrder.setStatus(etsBuildOrder.getStatus());
buildOrder.setIssued(etsBuildOrder.getIssued());
buildOrder.setTeam(etsBuildOrder.getTeam());
buildOrder.setType(etsBuildOrder.getType());
buildOrder.setBuildId(etsBuildOrder.getBuildRequestId());
buildOrder.setPartNumber(etsBuildOrder.getPartNumber());
buildOrder.setProductCode(etsBuildOrder.getProductCode());
buildOrder.setSpecialInstructions(etsBuildOrder.getSpecialInstructions());
buildOrder.setBoCreationDate(RestWsUtil.convertDateToString(etsBuildOrder.getCreationDate(), Constants.SIMPLE_DATE_FORMAT_DATE_ONLY));
buildOrder.setBoModifiedDate(RestWsUtil.convertDateToString(etsBuildOrder.getModifiedDate(), Constants.SIMPLE_DATE_FORMAT_DATE_ONLY));
buildOrder.setChangeHistory(etsBuildOrder.getChangeHistory());
}
JSON return format. boId is not located at the beginning of JSON:
{
"name": "TLO9009",
"factory": "L-Slider",
"type": null,
"boCreationDate": "18 Apr 2018",
"boModifiedDate": "18 Apr 2018",
"status": "Pending Approval",
"team": null,
"partNumber": null,
"specialInstructions": "Special Inst",
"changeHistory": "Pending ApprovalWed Apr 18 10:14:06 SGT 2018",
"productCode": null,
"issued": null,
"multifeature": null,
"buildId": 0,
"boId": 141
}
How the JSON should return. I would like the JSON to return like this:
{
"boId": 141
"name": "TLO9009",
"factory": "L-Slider",
"type": null,
"boCreationDate": "18 Apr 2018",
"boModifiedDate": "18 Apr 2018",
"status": "Pending Approval",
"team": null,
"partNumber": null,
"specialInstructions": "Special Inst",
"changeHistory": "Pending ApprovalWed Apr 18 10:14:06 SGT 2018",
"productCode": null,
"issued": null,
"multifeature": null,
"buildId": 0
}
I am new to JAVA and help is much appreciated.Thanks in advance.
That is not possible and not necessary. Both JSON objects are identical, there is not order.
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
https://json.org/

compare and combine two Objects in java which produces as JSON

I have two objects of type ClassA<List<List<String>>> which retruns as a JSON from controller. I want to merge these two objects based on two properties(country and operator) if they are equal. Example JSON looks like
#RequestMapping(value = "/controller3"method= RequestMethod.GET produces= MediaType.APPLIACTION_JSON_VALUE)
public ClassA<List<List<String>>> process(HttpServletRequest request) {
ClassA<List<List<String>>> one = service.getvalues();
ClassA<List<List<String>>> two = service.getvaluestwo();
return merge(one, two);
}
JSON1 looks like
"title": "Source",
"dateRange": "23 August 2017 - 21 November 2017",
"total": 336,
"pageSize": 336,
"pageNum": -1,
"nextPageKey": "",
"results": [
[
"India",
"Idea",
"30"
],
"headers": [
"Country",
"Operator",
"Count"
]
JSON2 looks like
"title": "Source",
"dateRange": "23 August 2017 - 21 November 2017",
"total": 336,
"pageSize": 336,
"pageNum": -1,
"nextPageKey": "",
"results": [
[
"India",
"Idea",
"20"
],
"headers": [
"Country",
"Operator",
"Count"
]
I want the Resultant JSON would extracted from RestController from the type ClassA<List<List<String>>> would looks like
"title": "Source",
"dateRange": "23 August 2017 - 21 November 2017",
"total": 336,
"pageSize": 336,
"pageNum": -1,
"nextPageKey": "",
"results": [
[
"India",
"Idea",
"30",
"20",
"50"
],
"headers": [
"Country",
"Operator",
"Count1",
"Count2",
"Total"
]
if there is no match then aggregate them as with zero count of each other.
Can you guys help me?

access elementsJSON array in Java

I am new to JSON. I know the forecast is an array, but how do I access elements date and text? I know where the problem is: It seems i cannot get access to forecast array, as this gives a null.
JSONArray jArr = data.getJSONArray("forecast");
what is the proper notation to get access to the forecast array. I was trying
`JSONArray jArr = data.getJSONObject("item").getJSONObject("condition").getJSONArray("forecast");
but this notation still did not give me access to the array. What notation will give me correct reference to forecast array?
"item": {
"title": "Conditions for Kingston, Saint Andrew, JM at 09:00 PM EST",
"lat": "18.015711",
"long": "-76.79731",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-109251/",
"pubDate": "Sat, 22 Oct 2016 09:00 PM EST",
"condition": {
"code": "27",
"date": "Sat, 22 Oct 2016 09:00 PM EST",
"temp": "77",
"text": "Mostly Cloudy"
},
"forecast": [
{
"code": "32",
"date": "20 Oct 2016",
"day": "Thu",
"high": "35",
"low": "26",
"text": "Sunny"
},
{
"code": "34",
"date": "21 Oct 2016",
"day": "Fri",
"high": "34",
"low": "28",
"text": "Mostly Sunny"
},
{
"code": "26",
"date": "22 Oct 2016",
"day": "Sat",
"high": "37",
"low": "32",
"text": "Cloudy"
},
{
"code": "23",
"date": "23 Oct 2016",
"day": "Sun",
"high": "37",
"low": "34",
"text": "Breezy"
},
I was trying something like this, but my android app was crashing? I just need help in accessing the elements in the array to set them on some textfields.
public void populate(JSONObject data) throws JSONException {
JSONArray jArr = data.getJSONArray("forecast");
for(int i=0;i<jArr.length();i++){
JSONObject jDayForecast = jArr.getJSONObject(i);
String date = jDayForecast.getString("date");
String text = jDayForecast.getString("text");
}
}
here is the error in stacktrace.
54.516 27220-27220/net.digitalphantom.app.weatherapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.digitalphantom.app.weatherapp, PID: 27220
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String net.JamCast.app.weatherapp.data.Forecast.getDate()' on a null object reference
at net.JamCast.app.weatherapp.WeatherActivity$override.serviceSuccess(WeatherActivity.java:363)
at net.JamCast.app.weatherapp.WeatherActivity$override.access$dispatch(WeatherActivity.java)
at net.JamCast.app.weatherapp.WeatherActivity.serviceSuccess(WeatherActivity.java:0)
at net.JamCast.app.weatherapp.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:95)
at net.JamCast.app.weatherapp.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:37)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
define a java bean class, use GSON jar translate json string to class object, then you can get text element by your object. please search in google sample.

Java - returning JSON from servlet with Gson library

I am using GSON library.
I have a programm which returns JSON.
JSON constructs and returns in this way:
Gson gson = new Gson();
//findNewComments returns List<Comment> comments
return gson.toJson(service.findNewComments(id,lastId));
So the result is:
[
{
"id": 43,
"entryId": 19,
"author": " m8w46",
"body": "mw86",
"date": "WED 9, 2011"
},
{
"id": 44,
"entryId": 19,
"author": " n7w4",
"body": "nw77w4",
"date": "WED 9, 2011"
}
]
But this array must be named as "comments"!
"comments": [
{
"id": 43,
"entryId": 19,
"author": " m8w46",
"body": "mw86",
"date": "WED 9, 2011"
},
{
"id": 44,
"entryId": 19,
"author": " n7w4",
"body": "nw77w4",
"date": "WED 9, 2011"
}
]
How can i do that?
Not sure if this is acceptable to you but:
public class CommentWrapper {
List<Comments> comments;
public CommentWrapper(List<Comment> comments) {
this.comments = comments;
}
}
Then you can do:
return new Gson().toJson(new CommentWrapper(service.findNewComments(id,lastId)));
Which results in:
{
"comments": [
....your data here...
]
}
Not sure if the object syntax is acceptable to you or not.
Aside from wrapper object, you can also use a simple java.util.Map with single entry, possibly bit less code.

Categories