0

I am trying to parse this JSON in Java.

{
 "json": {
 "abc": 0,
 "def": "100",
 "ghi": 1,
 "jkl": "0000000000",
 "mno": "3",
 "pqr": "COLS, COMPLETE",
 "stu": 2,
 "vwx": "0000010000",
 "yz": "00",
 "a123": 31,
 "b123": 1,
 "c123": "1270",
 "d123": "2",
 "e123": "00",
 "f123": 1,
 "g123": "0000001000"
 },
 "_indexes": {
 "c123": [
 "1270"
 ],
 "h123": [
 "1270"
 ]
 },
 "_d": false,
 "_dd": "2017-09-12T12:03:53.716Z",
 "_op": "add",
 "_id": 2
}

So far i have done the following using the gson library :-

 JsonParser par = new JsonParser();
 JsonElement jsonelement = par.parse(new FileReader("ddJob.json"));
 JsonArray jsonArr = jsonelement.getAsJsonArray();
 for(Object o : jsonArr)
 {
 JsonObject obj = (JsonObject)(o);
 JsonObject mArry = obj.getAsJsonObject();
 String keyVal = mArry.get("key").toString();
 if(keyVal.equalsIgnoreCase("\"jsonstore.LinkTree\""))
 {
 if(mArry.isJsonArray())
 {
 System.out.println("It is a JSONArray");
 }
 else if(mArry.isJsonObject())
 {
 System.out.println("It is a JSONObject"); 
 }
 else if(mArry.isJsonPrimitive())
 {
 System.out.println("It is a JSONPrimitive");
 }
 else if(mArry.isJsonNull())
 {
 System.out.println("It is a JSONNull");
 }
 }
 // Here it prints ,it is a JSONObject

I need help from here, I am not able to parse this JSONObject, I cannot get a JSonArray out of it and I am not able to get anything like a primitive etc. I can see in the debugger it has all the information I need. It is unreadable because of the spaces in between.

Can you please let me know how I can parse the rest of this JSON.

Thank you very much.

Henrik Aasted Sørensen
7,19611 gold badges55 silver badges66 bronze badges
asked Sep 21, 2017 at 13:06
8
  • 1
    The root element is not an array. Have you tried using getAsJsonObject (or whatever it is called) instead? Commented Sep 21, 2017 at 13:08
  • Sir, Thank you for your help. I notice that you have removed the last "_indexed" portion. That is fine. When I make the first call and treat it as a JsonObject and use the above call that you have mentioned. I get this error :- java.lang.IllegalStateException: Not a JSON Object: [{"key":"WL_WEBLOG_CONFIG","value":"{\u0000\"\u0000e\u0000n\ ............... Commented Sep 21, 2017 at 13:13
  • Accidentally sorted the properties during pretty-printing. That was unintentional. Fixed. Commented Sep 21, 2017 at 13:16
  • 2
    @KudmiSubba : The JSON in the error message is different from the JSON in your question. Commented Sep 21, 2017 at 13:17
  • 1
    ÿþ is 0xfffe in UTF-8; this is the byte order mark in UTF-16. You need to convert your string to UTF-8 Commented Sep 21, 2017 at 13:47

1 Answer 1

1

I used Java API for JSON Processing. Jar can be found here. This updated version should help with multiple JSON objects separated by commas. This version attempts to find the correct comma then splits the string based on those commas.

import java.io.StringReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParser.Event;
import static javax.json.stream.JsonParser.Event.KEY_NAME;
import static javax.json.stream.JsonParser.Event.VALUE_FALSE;
import static javax.json.stream.JsonParser.Event.VALUE_NUMBER;
import static javax.json.stream.JsonParser.Event.VALUE_STRING;
import static javax.json.stream.JsonParser.Event.VALUE_TRUE;
/**
 *
 * @author blj0011
 */
public class JSONParserTest {
 /**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 // TODO code application logic here
 String jsonString = "{\n" +
 " \"json\": {\n" +
 " \"abc\": 0,\n" +
 " \"def\": \"100\",\n" +
 " \"ghi\": 1,\n" +
 " \"jkl\": \"0000000000\",\n" +
 " \"mno\": \"3\",\n" +
 " \"pqr\": \"COLS, COMPLETE\",\n" +
 " \"stu\": 2,\n" +
 " \"vwx\": \"0000010000\",\n" +
 " \"yz\": \"00\",\n" +
 " \"a123\": 31,\n" +
 " \"b123\": 1,\n" +
 " \"c123\": \"1270\",\n" +
 " \"d123\": \"2\",\n" +
 " \"e123\": \"00\",\n" +
 " \"f123\": 1,\n" +
 " \"g123\": \"0000001000\"\n" +
 " },\n" +
 " \"_indexes\": {\n" +
 " \"c123\": [\n" +
 " \"1270\"\n" +
 " ],\n" +
 " \"h123\": [\n" +
 " \"1270\"\n" +
 " ]\n" +
 " },\n" +
 " \"_d\": false,\n" +
 " \"_dd\": \"2017-09-12T12:03:53.716Z\",\n" +
 " \"_op\": \"add\",\n" +
 " \"_id\": 2\n" +
 "},\n" +
 "{\n" +
 " \"json\": {\n" +
 " \"abc\": 0,\n" +
 " \"def\": \"100\",\n" +
 " \"ghi\": 1,\n" +
 " \"jkl\": \"0000000000\",\n" +
 " \"mno\": \"3\",\n" +
 " \"pqr\": \"COLS, COMPLETE\",\n" +
 " \"stu\": 2,\n" +
 " \"vwx\": \"0000010000\",\n" +
 " \"yz\": \"00\",\n" +
 " \"a123\": 31,\n" +
 " \"b123\": 1,\n" +
 " \"c123\": \"1270\",\n" +
 " \"d123\": \"2\",\n" +
 " \"e123\": \"00\",\n" +
 " \"f123\": 1,\n" +
 " \"g123\": \"0000001000\"\n" +
 " },\n" +
 " \"_indexes\": {\n" +
 " \"c123\": [\n" +
 " \"1270\"\n" +
 " ],\n" +
 " \"h123\": [\n" +
 " \"1270\"\n" +
 " ]\n" +
 " },\n" +
 " \"_d\": false,\n" +
 " \"_dd\": \"2017-09-12T12:03:53.716Z\",\n" +
 " \"_op\": \"add\",\n" +
 " \"_id\": 2\n" +
 "},\n" +
 "{\n" +
 " \"json\": {\n" +
 " \"abc\": 0,\n" +
 " \"def\": \"100\",\n" +
 " \"ghi\": 1,\n" +
 " \"jkl\": \"0000000000\",\n" +
 " \"mno\": \"3\",\n" +
 " \"pqr\": \"COLS, COMPLETE\",\n" +
 " \"stu\": 2,\n" +
 " \"vwx\": \"0000010000\",\n" +
 " \"yz\": \"00\",\n" +
 " \"a123\": 31,\n" +
 " \"b123\": 1,\n" +
 " \"c123\": \"1270\",\n" +
 " \"d123\": \"2\",\n" +
 " \"e123\": \"00\",\n" +
 " \"f123\": 1,\n" +
 " \"g123\": \"0000001000\"\n" +
 " },\n" +
 " \"_indexes\": {\n" +
 " \"c123\": [\n" +
 " \"1270\"\n" +
 " ],\n" +
 " \"h123\": [\n" +
 " \"1270\"\n" +
 " ]\n" +
 " },\n" +
 " \"_d\": false,\n" +
 " \"_dd\": \"2017-09-12T12:03:53.716Z\",\n" +
 " \"_op\": \"add\",\n" +
 " \"_id\": 2\n" +
 "}"; //I am guessing this is how your data looks.
 List<Integer> positions = new ArrayList();
 Pattern p = Pattern.compile("\\},\n\\{");//Find this pattern ***MAKE SURE THIS PATTERN IS CORRECT FOR YOU*** Your patter may be "\\},\\{"
 Matcher m = p.matcher(jsonString);
 while(m.find())
 {
 positions.add(m.start() + 1);//save starting position of the found pattern
 }
 System.out.println("# of positions: " + positions.size());
 List<String> jsonStringObjects = new ArrayList();
 if(positions.size() >= 1)
 {
 jsonStringObjects.add(jsonString.substring(0, positions.get(0)));//get first jsonString
 //System.out.println(jsonString.substring(0, positions.get(0))); 
 jsonStringObjects.add(jsonString.substring(positions.get(positions.size() - 1) + 1));//get last jsonString
 //System.out.println(jsonString.substring(positions.get(positions.size() - 1) + 1)); 
 }
 if(positions.size() >= 2 )
 for(int i = 0; i < positions.size() - 1; i++)//get all jsonStrings between first and last
 { 
 jsonStringObjects.add(jsonString.substring(positions.get(i) + 1, positions.get(i + 1)));
 //System.out.println(jsonString.substring(positions.get(i) + 1, positions.get(i + 1))); 
 } 
 System.out.println("# of jsonStringObjects: " + jsonStringObjects.size());
 int counter = 0;
 for(String item : jsonStringObjects)
 {
 System.out.println("JSON Object #: " + ++counter);
 try (JsonParser parser = Json.createParser(new StringReader(item))) {
 while (parser.hasNext()) {
 final Event event = parser.next();
 switch (event) {
 case KEY_NAME:
 String key = parser.getString();
 System.out.println("\t" + key);
 break;
 case VALUE_STRING:
 String value = parser.getString();
 System.out.println("\t" + value);
 break;
 case VALUE_NUMBER:
 BigDecimal number = parser.getBigDecimal();
 System.out.println("\t" + number);
 break;
 case VALUE_TRUE:
 System.out.println("\t" + true);
 break;
 case VALUE_FALSE:
 System.out.println("\t" + false);
 break;
 }
 }
 }
 }
 }
}

Output:

run:
# of positions: 2
# of jsonStringObjects: 3
JSON Object #: 1
 json
 abc
 0
 def
 100
 ghi
 1
 jkl
 0000000000
 mno
 3
 pqr
 COLS, COMPLETE
 stu
 2
 vwx
 0000010000
 yz
 00
 a123
 31
 b123
 1
 c123
 1270
 d123
 2
 e123
 00
 f123
 1
 g123
 0000001000
 _indexes
 c123
 1270
 h123
 1270
 _d
 false
 _dd
 2017年09月12日T12:03:53.716Z
 _op
 add
 _id
 2
JSON Object #: 2
 json
 abc
 0
 def
 100
 ghi
 1
 jkl
 0000000000
 mno
 3
 pqr
 COLS, COMPLETE
 stu
 2
 vwx
 0000010000
 yz
 00
 a123
 31
 b123
 1
 c123
 1270
 d123
 2
 e123
 00
 f123
 1
 g123
 0000001000
 _indexes
 c123
 1270
 h123
 1270
 _d
 false
 _dd
 2017年09月12日T12:03:53.716Z
 _op
 add
 _id
 2
JSON Object #: 3
 json
 abc
 0
 def
 100
 ghi
 1
 jkl
 0000000000
 mno
 3
 pqr
 COLS, COMPLETE
 stu
 2
 vwx
 0000010000
 yz
 00
 a123
 31
 b123
 1
 c123
 1270
 d123
 2
 e123
 00
 f123
 1
 g123
 0000001000
 _indexes
 c123
 1270
 h123
 1270
 _d
 false
 _dd
 2017年09月12日T12:03:53.716Z
 _op
 add
 _id
 2
answered Sep 21, 2017 at 13:44
Sign up to request clarification or add additional context in comments.

9 Comments

Sir , I am trying your suggestion, I will update you in a few minutes. cricket_007 -- I thank you for that, I didnt realize it may be UTF-16. Thanks.
@Sedrick Jefferson - Sir, This is working well, but can you please let me know how I could read an entire JSON file using this JsonParser. The issue is, when the entire record is read ( as above ) that is one record, and then there is a comma , that starts a new record like , {"json":... , at this point, the code throws an exception "Invalid token=COMMA ............ " . I checked the line number and column no that it suggested, it is indeed the comma separator. I will research this issue, but if you know this can you please suggest how I work around this. Thank you very much.
I am guessing that the comma causes the JSON to be invalid. What I would do is read the whole file to String. Next, I would split the String on the comma. Finally, I would handle each split ArrayList/ Array of Strings individually.
Suggested won't work. There are a lot of commas in the JSON and you only want to split on a specific one.
Thank you. I will try to split on ",{" and see if that works. It really helped me. Thanks.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.