0

Let me give some background information:

I have an org.json JSONObject which has all of the values of this JSON object, taken from an API.

"socialMedias" : {
    "TWITTER" : "[twitter url]",
    "YOUTUBE" : "[youtube url]",
    "INSTAGRAM" : "[instagram url]",
    "FACEBOOK" : null,
    "VIMEO" : "[vimeo url]",
    "LINKEDIN" : "[linkedin url]"
}

The thing is, however, that while in one of these calls to the API, there is only one value that is null, but the same value isn't always null, and there can be more than one values that ARE null; Let me illustrate what I mean:

"socialMedias" : {
    "TWITTER" : "[twitter url]",
    "YOUTUBE" : null,
    "INSTAGRAM" : null,
    "FACEBOOK" : "[facebook url]",
    "VIMEO" : "[vimeo url]",
    "LINKEDIN" : "[linkedin url]"
}

That above is something the API could return. Another one:

"socialMedias" : {
    "TWITTER" : null,
    "YOUTUBE" : null,
    "INSTAGRAM" : null,
    "FACEBOOK" : null,
    "VIMEO" : null,
    "LINKEDIN" : null
}

Literally every value in the JSONObject can be null!

Back to the question: How would I be able to check which values of the JSONObject are null, and only return the ones that are NOT null; So for the very first code block I had, the code would return the TWITTER, YOUTUBE, INSTAGRAM, VIMEO and LINKEDIN URLs, and exclude the FACEBOOK, one because the value of that one is null.

This API returns a JSONObject of the social medias that a user has linked to their account, and puts null if the user hasn't added a value, if it wasn't clear.

I am doing this in Java 17, using the latest version of the org.json library. Thanks in advance for any help, appreciate it!

Edit: guess i just wasn't using my brain in this post. i realize now that i could just have looped through each value in the socialMedias object and just added all the ones that weren't null into a list... sorry!

InsertIGN
  • 29
  • 1
  • 5
  • Why do you care? The client (user) of that object is still going to have to determine which of the possibilities is/are present. What's wrong with just ignoring the null values? Sounds like you're making work for yourself. – Jim Garrison Oct 30 '21 at 00:04
  • With Jackson that is easy, with `org.json` I am not sure you can do it. – João Dias Oct 30 '21 at 00:14
  • @JimGarrison I want to just return the values that are not null; I am using an API to return a message over to a different server, and that API will throw a fit if there are null values. :/ – InsertIGN Oct 30 '21 at 00:26
  • @JoãoDias Never used Jackson, only ever used org.json. Also, I have written an insane amount of code in org.json, it would take months to change everything. :/ – InsertIGN Oct 30 '21 at 00:27
  • That is maybe the original issue. I've never used `org.json` but it seems to me that Jackson is way simpler to use and more robust. Refer to https://stackoverflow.com/questions/42641301/org-json-jsonobject-vs-gson-library-jsonobject for example. – João Dias Oct 30 '21 at 00:34

1 Answers1

0
    JSONObject socialMedias = new JSONObject(apiResponse).getJSONObject("socialMedias");
    JSONArray socialMediaArray = socialMedias.toJSONArray(socialMedias.names());
    socialMediaArray.toList().stream().filter(v -> v != null);
Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
somayaj
  • 56
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 30 '21 at 17:28
  • Hey This doesn't seem to work; I get a ``java.util.Stream.ReferencePipeline$2@`` plus a bunch of numbers at the end. I tried to filter the values and perform #toString to turn the Stream to a String, and I got that error. Am I doing something wrong? – InsertIGN Oct 30 '21 at 22:33
  • private static Stream buildResponse() { JSONObject apiJsonResponse = null; try { JSONObject socialMedias = new JSONObject(apiResponse).getJSONObject("socialMedias"); JSONArray socialMediaArray = socialMedias.toJSONArray(socialMedias.names()); return socialMediaArray.toList().stream().filter(v -> v != null); } catch (JSONException ex) { } } public static void main(String... args) { buildResponse().forEach(System.out::println); } – somayaj Oct 31 '21 at 00:46
  • I'm on java 15 here if that helps. works fine for me, here's the output .m2/repository/org/json/json/20210307/json-20210307.jar JsonParser [facebook url] [linkedin url] [vimeo url] [twitter url] – somayaj Oct 31 '21 at 00:46
  • trying this with java 17, maybe somethings's different there. will send out an update – somayaj Oct 31 '21 at 00:49
  • works on 17 as well. the code is really simple should work as-is. – somayaj Oct 31 '21 at 01:24