0

this is a prices.json file which contains many products prices i need iterate over this .json in order to compare with another json output from another system

i need to get only the fields "MATNR" field which contains the product ID and "NET_PRICE" = contain the price before VAT and "PRICE_INC_TAX" = contain the price after VAT

  [
    {
        "customer": {
            "AUART": "",
            "KALSM": "ZDPS00",
            "KUNNR": "0001234567",
            "SPRAS": "",
            "VKGRP": "",
            "VKORG": "1000",
            "VTWEG": "10",
        "ZMDD": "99",
        "ZTERM": ""
    },
    "outdated": false,
    "outputs": [
        {
            "KUNNR": "0001234567",
            "KWMENG": "1",
            "MATNR": "000000000001000001",
            "NET_PRICE": "15.50",
            "NET_PRICE_PER_BASE_UNIT": "15.50",
            "ORDER": "1",
            "PRICE_INC_TAX": "18.13",
            "PRICE_INC_TAX_PER_BASE_UNIT": "18.13",
            "TOTAL_TIME": "Total Calculation Time: 15ms",
            "VRKME": "EA",
            "converted_quantity": "1.00"
        },
        {
            "KUNNR": "0001234567",
            "KWMENG": "1",
            "MATNR": "000000000001000002",
            "NET_PRICE": "20.00",
            "NET_PRICE_PER_BASE_UNIT": "20.00",
            "ORDER": "1",
            "PRICE_INC_TAX": "23.40",
            "PRICE_INC_TAX_PER_BASE_UNIT": "23.40",
            "TOTAL_TIME": "Total Calculation Time: 12ms",
            "VRKME": "EA",
            "converted_quantity": "1.00"
        },
        {
            "KUNNR": "0001234567",
            "KWMENG": "1",
            "MATNR": "000000000001000003",
            "NET_PRICE": "20.00",
            "NET_PRICE_PER_BASE_UNIT": "21.00",
            "ORDER": "1",
            "PRICE_INC_TAX": "24.40",
            "PRICE_INC_TAX_PER_BASE_UNIT": "24.40",
            "TOTAL_TIME": "Total Calculation Time: 12ms",
            "VRKME": "EA",
            "converted_quantity": "1.00"
        }
      ]
    }
]

this is the block of code I'm trying get the specific values but it's still not works:

public static void parseJson() {
    JSONParser jsonP = new JSONParser();

    try (FileReader reader = new FileReader("MyJson.json")) {
        // Read JSON File
        Object obj = jsonP.parse(reader);

         JSONArray priceList = ((JSONArray) obj).get("outputs"); 
         //1) getting error above line "The method get(int) in the type ArrayList is not applicable for the arguments (String)"

         //2) TODO: here I want to iterate only the "MATNR", "NET_PRICE", PRICE_INC_TAX values

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

I'll be so thankful who can solve me two questions on this method! Thanks in advance

Haim Sabag
  • 133
  • 2
  • 12
  • 1
    *Best practice:* Use a **JSON parser**. Other than that, there is no "best practice", just many different JSON libraries that can be used in various ways. – Andreas Jun 07 '20 at 12:53
  • under the Link to How to iterate over a JSONObject? there's another link to code: – Haim Sabag Jun 08 '20 at 07:01
  • Since the JSON text starts with a `[`, it's a JSON Array, and you correctly cast the root object to `JSONArray`. The value of `outputs` also starts with `[`, so why are you casting `emp.get("outputs")` to `JSONObject`, when it is obviously an array? – Andreas Jun 08 '20 at 09:16
  • *FYI:* There are so many Java JSON libraries, so please specify which one you're using. – Andreas Jun 08 '20 at 09:17
  • uses: import org.json.simple.parser.JSONParser; com.googlecode.json-simple json-simple 1.1.1 – Haim Sabag Jun 08 '20 at 14:59
  • actually I need run in a loop over all "outputs" and on each iteration gets only "MATNR", and "NET_PRICE" values then compare the with another variables to be validated. If someone can help me solve it I really appreciate! – Haim Sabag Jun 08 '20 at 15:04
  • Change `(JSONObject) emp.get("outputs")` to `(JSONArray) emp.get("outputs")`, and add the loop. --- Don't show `import` and `` in a comment. Edit the question and show it there. Delete that comment. – Andreas Jun 09 '20 at 04:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215559/discussion-between-haim-sabag-and-andreas). – Haim Sabag Jun 09 '20 at 06:45
  • Why did you take working code (`JSONArray empList = (JSONArray) obj;`) and change it to non-working code (`JSONObject empList = (JSONObject) obj;`)? – Andreas Jun 09 '20 at 09:26
  • when I changed to: `JSONArray empList = (JSONArray) obj;` this line show me the next error `JSONArray empObj = (JSONArray) empList.get("outputs");` "The method get(int) in the type ArrayList is not applicable for the arguments (String)" – Haim Sabag Jun 11 '20 at 06:37
  • Which is why you originally *iterated* the `JSONArray` using `empList.forEach(...)`. You totally ruined the working code in `main` in your attempt to fix the error in the first statement inside `parseEmpObj()`. Take a step back and **(re)think** what you're trying to do. – Andreas Jun 11 '20 at 07:28
  • seems you not answering my updated code, see my last changes in question it is summarised to final result and still need 2 questions to solve. thanks a lot in advance dear Andreas – Haim Sabag Jun 11 '20 at 09:13
  • Seems you changed the question *after* my comment. As I've repeatedly said, you originally had partially working code, that correctly found the `outputs` value but failed to process that value correctly. Now your updated code can't even process the root array, meaning you've leaped a huge step *backwards*. I give up, since you don't seem to heed any of the advice given. – Andreas Jun 11 '20 at 20:21
  • I decided to summarise to code since after all your advices need to change according your suggestion - the updated code is the last modified version. – Haim Sabag Jun 14 '20 at 06:37

1 Answers1

0

We use the javax.json.* library for doing that kind of stuff.

 try(JsonReader jsonReader = Json.createReader(new StringReader(myJsonString))) {
    JsonObject jsonObject = jsonReader.readObject();
    return jsonObject.getJsonArray("outputs");
    // TODO: don't return something, instead iterate over array and get attributes from objects.
 }
 catch(Exception exception) {
    // TODO: Do something with exception
 }

I hope that helps.