0

first of all.. If there is missing some information just ask and I will give the needed input.

I have a .json file on my Desktop, and now I have to use java and have to extract values out of the .json file. This is my task: **write a script which extract the value “automatic -> kernel -> release” out of the attached json file.

example: output:

3.16.0-5-amd64**

The json file has 27k+ lines, I don't know if this can be a problem or not.

the .json file includes this:

   1 {
   2   “results”: 3,
   3   “rows”: [
   4     {
   5   “name”: “test”,
   6   “chef_environment”: “office”,
   7   “json_class”: “Chef::Node”,
   8   “automatic”: {
   9     “nginx”: {
  10       “version”: null,
  11       “configure_arguments”: [
  12
  13       ],
  14       “prefix”: null,
  15       “conf_path”: null
  16     },
.....
  98 “kernel”: {
  99 “name”: “Linux”,
 100 “release”: “3.16.0-5-amd64"

And this is my code now:

package json;

import java.io.*;
import java.io.File;
import java.io.FileReader;
import org.json.simple.parser.ParseException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.JSONArray;
import java.util.Iterator;

public class json {

    public static void main(String[] args) throws Exception {
        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(new FileReader("C:\\Users\\admin-elias\\Desktop\\nginx.json"));
            JSONObject jsonObject = (JSONObject) obj;

            String automatic = (String) jsonObject.get("automatic");
            String kernel = (String) jsonObject.get("kernel");
            JSONArray release = (JSONArray) jsonObject.get("release");

            System.out.println("automatic: "+automatic);
            System.out.println("kernel: "+kernel);
            System.out.println("release: "+release);
            ;



            } catch (Exception e) {
                e.printStackTrace();
            }
    } 
}

When I try to run the program, I get the following error:

java.lang.NumberFormatException: For input string: "18446744073709551615"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Long.parseLong(Long.java:692)
    at java.base/java.lang.Long.valueOf(Long.java:1144)
    at org.json.simple.parser.Yylex.yylex(Yylex.java:660)
    at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
    at json.json.main(json.java:18)

What am I doing wrong? I am relatively new to java and I need to get this program running so every suggestion or help would be very nice. :)

Edit:

I finally got my code which works just perfectly:

public class json {

    public static void main(String[] args) throws Exception {
        try {
            JsonReader jsonReader = new JsonReader(new FileReader("filename"));
            Gson gson = new Gson();

            JsonElement json = gson.fromJson(jsonReader, JsonElement.class);

            //JsonElement json = new JsonParser().parse(jsonString);

            JsonArray array = json.getAsJsonObject().get("rows").getAsJsonArray();
            int length = array.size();
            for (int i = 0; i < length; i++) {
                System.out.println(array.get(i).getAsJsonObject().get("automatic").getAsJsonObject().get("kernel").getAsJsonObject().get("release").getAsString());
            }

        } catch (IOException e) {
                e.printStackTrace();
            }
    }
}
Ch0p
  • 11
  • 1
  • 7
  • 2
    this number 18446744073709551615 does not fit into a long – nsawaya Mar 19 '19 at 12:49
  • Gson library supports big integers out of the box. Is it possible for you to use Gson instead of simple parser? https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5 – Patrick Dorn Mar 19 '19 at 13:14
  • Well @PatrickDorn Then I guess I have to write a complete new code? Haven't used Gson yet but maybe it's worth giving it a try. – Ch0p Mar 19 '19 at 13:17
  • Shouldn't be too hard. Gson supports FileReader as input and has its own com.google.gson.JsonObject and com.google.gson.JsonArray classes. For parsing: JsonObject obj = gson.fromJson(fileReader, JsonObject.class); – Patrick Dorn Mar 19 '19 at 13:28
  • Okay cool @PatrickDorn thank you for the help, I will try that out :) – Ch0p Mar 19 '19 at 13:31

2 Answers2

2

The value

18446744073709551615

exceeds the maximum value of a long:

9223372036854775807

You should parse it into a BigInteger instead.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • Okay, that's good to know. How can I parse into a BigInteger? What do I have to change in my code? – Ch0p Mar 19 '19 at 12:50
  • Well ... According to ["exceeding LONG.MAX_VALUE in JSON Simple parser"](https://stackoverflow.com/questions/24064028/exceeding-long-max-value-in-json-simple-parser) this does not seem to be possible with JSON Simple. So you must look for another parser. – Seelenvirtuose Mar 19 '19 at 13:28
  • Okay.. well.. I will try, thank you :) – Ch0p Mar 19 '19 at 13:29
  • @Ch0p, Can you please assist me in declaring the 18446744073709551615 which 2^64 - 1 value in Java? – Bhala T R Oct 27 '22 at 09:19
0

it's trying to parse the value as an integer / long and it's outside the range

18446744073709551616 = 2^64 - 1

Integer.MAX_VALUE = 2^31 - 1

Long.MAX_VALUE = 2^63 - 1

lolo
  • 17,392
  • 9
  • 25
  • 49
  • Okay, that makes sense. But what can I change in my code to avoid this problem? – Ch0p Mar 19 '19 at 12:55
  • are you using org.json.simple? – lolo Mar 19 '19 at 12:59
  • Yes, as you can already see in my imports at the top :) – Ch0p Mar 19 '19 at 12:59
  • try to use org.json.JSONObject instead of org.json.simple.JSONObject . it's 2 different libraries – lolo Mar 19 '19 at 13:00
  • Okay, did that, but the error still remains :/ – Ch0p Mar 19 '19 at 13:03
  • Do you have more information? which variable you trying to set? did you post all the code? – lolo Mar 19 '19 at 13:05
  • Yes, the code here is the exact same as in my eclipse. Well I noticed that the error slightly changed. the last line of the error `java.lang.NumberFormatException: For input string: "18446744073709551615"....at json.json.main(json.java:18)` has changed to `at json.json.main(json.java:17)` – Ch0p Mar 19 '19 at 13:08
  • So I guess it's something at line 17 which is this: `Object obj = parser.parse(new FileReader("C:\\Users\\admin-elias\\Desktop\\nginx.json"));` – Ch0p Mar 19 '19 at 13:09
  • Try change the list object value to one value. “automatic”: "1" – lolo Mar 19 '19 at 13:26