0

I have a log file and each sentence contains a JSON object as per given below: -

{"action": "follow", "target": "FabianoCaruana", "timestamp": 1487149397523, "user": "GMHikaru"}
{"action": "tweet", "id": 16606634614844517897, "message": "woop #ruby", "timestamp": 1487149445603, "user": "hugopeixoto"}
{"action": "retweet", "target_id": 16606634614844517897, "timestamp": 1487149446020, "user": "spambot"}
{"action": "retweet", "target_id": 16606634614844517897, "timestamp": 1487149446592, "user": "noob_saibot"}
{"action": "tweet", "id": 14936153188153171323, "message": "woop #vim", "timestamp": 1487149463067, "user": "eevee"}
{"action": "follow", "target": "eevee", "timestamp": 1487149466209, "user": "pkoch"}
{"action": "tweet", "id": 1801829421162967878, "message": "woop #processes", "timestamp": 1487149468671, "user": "r00k"}
{"action": "retweet", "target_id": 1801829421162967878, "timestamp": 1487149469555, "user": "noob_saibot"}
{"action": "follow", "target": "r00k", "timestamp": 1487149472418, "user": "pkoch"}

I am reading each line using JSON parser and storing into a String for further processing

String line = {"action": "tweet", 
               "id": 16606634614844517897, 
               "message": "woop #ruby", 
               "timestamp": 1487149445603, 
               "user": "hugopeixoto"};

while parsing above String using "json-simple-1.1.1.jar", I am getting an error : -

java.lang.NumberFormatException: For input string: "16606634614844517897"

Code to parse above JSON string: -

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(line);
String name =(String) json.get("user");

I want to print the "user" name present in above JSON string.

Obviously, the id is a Long format, hence the exception is.

Could you please suggest me a workaround to eliminate above exception. I tried all combinations but didn't work.

Kushal Shinde
  • 715
  • 7
  • 15
  • 2
    Why not make that `id` a String? Does it have to be numeric? The problem is that that number exceeds the `Long` range. You can see that by running `new BigInteger("16606634614844517897").subtract(new BigInteger(Long.toString(Long.MAX_VALUE)))` – ernest_k May 26 '18 at 14:16
  • Are you sure the exception is thrown for that line? – NielsNet May 26 '18 at 14:16
  • 2
    Possible duplicate of [Error occurred While try to parsing JSON String using java](https://stackoverflow.com/questions/30183075/error-occurred-while-try-to-parsing-json-string-using-java) – ernest_k May 26 '18 at 14:24
  • 1
    Thank you @ErnestKiwele. Your posted link helped me to resolve this issue. I switched to Gson. This problem arose because of the limitation of json-simple 1.1.1. – Kushal Shinde May 28 '18 at 15:31

1 Answers1

2

Obviously, the id is a Long format

Nope, sorry the maximum Long value is the following :

9223372036854775807 <-- has a length of 19

while yours

16606634614844517897 <- has a length of 20

I see two simple solutions to your problem.

If the id only serves the purpose of unique identification (which is probably the case here), you should be using a String instead?

If you'll need to use the id and compare its numeric value or perform numeric operations on it, which I doubt, you could still use a BigInteger like the following

new BigInteger("16606634614844517897");
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • 1
    Thanks for your help. This problem was due to the limitation of json-simple 1.1.1. I switched to gson and my problem got resolved. – Kushal Shinde May 28 '18 at 15:35