Reputation: 6680
I have json with different fieldNames carrying the values of my interest. They come from different sources. I want to get those fields of my interest into one common name. For example:
This is the raw json:
{"movieName":"A","Leadactor":"","leadActress":"","movieTitle":"","hero":"","heroine":"","source":"IMDB"}
{"movieName":"","Leadactor":"","leadActress":"","movieTitle":"B","hero":"B1","heroine":"B2","source":"Netflix"}
{"movieName":"C","Leadactor":"C1","leadActress":"C2","movieTitle":"","hero":"","heroine":"","source":"IMDB"}
{"movieName":"D","Leadactor":"D1","leadActress":"D2","movieTitle":"","hero":"","heroine":"","source":"IMDB"}
{"movieName":"","Leadactor":"","leadActress":"","movieTitle":"E","hero":"E1","heroine":"E2","source":"Netflix"}
for source imdb the fields of my interest are different from the fields of interest for source netflix. My final goal is to show them in table in front end. So trying to get my fields of interest into common fields.
i want to convert it into:
{Name:"A",Actor"A1",Actress"A2","source":"IMDB"}
{Name:"B",Actor"B1",Actress"B2","source":"Netflix"}
{Name:"C",Actor"C1",Actress"C2","source":"IMDB"}
{Name:"D",Actor"D1",Actress"D2","source":"IMDB"}
{Name:"E",Actor"E1",Actress"E2","source":"Netflix"}
The solution i am thinking is to use regex to remove all fields with empty values then replace movieName and movieTitle to Name. similarly for other fields.
Is there a much better way to handle this problem.( The json i get is very huge. So looking for super fast way to preprocess it.)
Upvotes: 2
Views: 1189
Reputation: 1541
JSON has context free grammar. And REGEX cannot parse such grammars reliably.
For example this JSON is impossible to parse reliably with regex :
{movie : "Apocalypse Now", director : {first_name: "Francis", last_name: "Ford Coppola"}}
Because it has nested curly bracket, regex will match next to last bracket as closing in whole expression. In other words , when there is nested expressions (JSON, XML, javascript etc) use specialized parser, because regex just can not handle it.
Upvotes: 1
Reputation: 29874
Use a JSON Streaming parser like json-simple or Jackson.
They are really fast, memory efficient, saves you tons of encoding headaches and are easy to implement on simple structures like yours.
Upvotes: 1
Reputation: 785866
My advise is to use gson and covert above JSON string to a Map like this:
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> map =
gson.fromJson("{'key1':'foo','key2':'bar', 'key3', ''}", type);
Once you get your map then simlpy iterate and remove all elements with empty values. After that you can generate your JSON string back from this map.
Upvotes: 1
Reputation: 9286
You can easily do that by creating your own JSON "adder?" json.org
has a good JSON library, your code would look something like this.
public static void addJSON(JSONObject one, JSONObject two){
while(one.keys().hasNext()){
String key = (String) one.keys().next();
try{
if(one.getString(key).equals("")){
one.put(key, two.get(key));
}
}catch(JSONException e){
}
}
}
Upvotes: 1