I have been tasked with developing a java application that includes retrieving one property from a json response from a web server.

Normally when I parse json I go with the standard way of creating a pojo model of the json and then using the google gson library to create the object. However this time I feel that it is overkill to have to create a pojo when I am just retrieving one property from the json, and I want to use a workaround that will just splice and dice the string until I am left with the property that I want.

So my question is, would there be any downsides with doing such a workaround instead of going the standard way of using a pojo and gson?

  • 1
    You might find JMESpath a useful alternative. AWS uses it for their command-line tools, so I'm assuming it's fairly well shaken-out. And I'm sure there are alternative XPath-like query tools. – kdgregory Aug 8 '17 at 0:05
  • 3
    I'm sure there are Java libraries that let you deserialize into generic representation which you can then navigate. In fact, I'm willing to bet despite never having used it, that with gson you can simply do fromJson into a JsonObject or JsonArray to accomplish this. – Derek Elkins left SE Aug 8 '17 at 2:22
  • 3
    This is effectively the same as 'can I parse XML with a regex', and the well established answer is: Don't do it! You (or your replacement) will regret it later. Yes, dragging in all the parsing machinery is annoying, but just do it - you'll thank yourself later. – Michael Kohne Aug 8 '17 at 10:09
  • In addition to Derek Elkins' comment, take a look here. This question basically boils down to Are there any downsides with reinventing the wheel?. – Laiv Aug 8 '17 at 12:19
  • 1
    Is there a specific reason your question gives the impression the only alternative to "making a pojo model" is "writing your own parser"? Or do you mean something different by "workaround that will just splice and dice the string"? – Doc Brown Aug 8 '17 at 13:08

Yes there is a downside: bugs in your half baked parser. Sure JSON is simple but you still can encounter tricky escaping situations.

Just use an existing tried and true JSON parsing method and save your brain cells for the harder problems.

| improve this answer | |
  • 9
    Plus the wasted effort as soon as you do need another property from the JSON; throw out custom solution and do what should have been done to begin with. – Andy Aug 8 '17 at 0:59

To avoid creating a new Java class which maps the complete JSON response when you only need one single field of it, you could use a library like JsonSimple which represents a parsed JSON object with a HashMap. You can then retrieve the value of that one key from that HashMap without mentioning anything else it contains.

| improve this answer | |

Premature optimization.

The major downside is the time wasted on a home made solution.

First, you'll spend time developing your solution, and then fixing bugs. If bugs there are, it may waste not only your time, but also the time of your coworkers: other developers, testers, support personnel, etc.

Then, everyone who has to read your code will have to spend extra time understanding what's happening here. One knows how to deserialize a JSON, but no other persons will be familiar with your approach.

Ideally, requirements will never change and your code won't have to be changed. In real life, things may not go well though. If requirements change (such as having to deserialize other fields or adapting your code to a change in JSON structure), it could cascade into more developer's time, more code, more bugs.

Let's say you're really fast, and really good. You finished implementing your solution in one hour. What's your hourly wage? Now in terms of saved CPU cycles, how much your custom solution saves to your employer? You get the picture.

| improve this answer | |

The downside is your code will throw exceptions when you attempt to access a property which doesn't exist, rather than when you attempt to parse the json.

I do have a lot of sympathy for your dilemma. I had to consume a json model from a graphing plug in in c#

I went the normal way of creating classes to match the object only to find that the writer of the graphing app had fully used the 'ability' of JavaScript not to strongly type.

This resulted in an impossible to recreate model as property X could be one of 5 different models each having their own sub objects which also varied.

Fortunately c# now has the dynamic class which i was able to use. I don't believe java has an equivalent though. Possibly you could deserialise to a nested Hashtable?

| improve this answer | |

A compact JSON parsing library is JSON-java. It doesn't map to POJO classes, it gives you a navigable representation of the parsed JSON. It's self-contained, meaning doesn't depend on other libraries (a fact that I like very much). I've used it a few times when a full fledged JSON/POJO mapper seemed overkill to me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.