0

I have a program that interfaces with an API which outputs JSON responses.

Considering I want to extract some specific keys and values from the JSON to use in my program, but at a later point I may want to expand on this, what is the most resource efficient way to parse the JSON for use within the program? I'm using JsonSlurper to work with the output.

  • Make a configuration option to list which keys I want to pull from the JSON. Iterate through that list and create a Map to hold the data, which is then used throughout the program.

  • Hard code the wanted keys into a class and have a method to access the corresponding data from the JSON object, then assign the key and value to a final variable. EG: public final String orderId = jsonParser.findByKey("orderId"), which then iterates the JSON document to find that key

Or is there better practice way to do this? Ideally it should be extendible as easily as possible in case more data is to be pulled from the JSON.

Donglecow
  • 507
  • 4
  • 22
  • 2
    what do you mean by "efficient"? Is it memory consumption? speed of response? CPU utilization? readability? You'll get different answers depending on the performance characteristics you may be looking at. – Andres Almiray Mar 16 '17 at 13:36
  • Good question, I hadn't considered the different definitions of efficiency in this context. I meant memory consumption and CPU utilisation. I want to host this on a Raspberry Pi at some point, so I want to use as little hardware resources as possible. – Donglecow Mar 16 '17 at 14:06

0 Answers0