You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
(JSONArray).toList() breaks format of entries #737
Unfortunately invoking method .toList() on JSONArray Object causes entries of the JSONArray to become malformated.
Issue examples:
Output from (JSONArray).get() {"data":{"pressure_avg":1012.3399999999999,"humidity_avg":61.5,"temperature_avg":12.85,"pm10_avg":0.6900000000000001,"pm25_avg":0.36},"school":{"city":"DŁUGOŁĘKA","street":null,"post_code":"19-111","latitude":"53.2753471","name":"SZKOŁA PODSTAWOWA W DŁUGOŁĘCE","longitude":"22.8683732"},"timestamp":"2023-05-19 23:56:45"}
Output from (JSONArray).toList().get() {data={pressure_avg=1012.3399999999999, humidity_avg=61.5, temperature_avg=12.85, pm10_avg=0.6900000000000001, pm25_avg=0.36}, school={city=DŁUGOŁĘKA, street=null, post_code=19-111, latitude=53.2753471, name=SZKOŁA PODSTAWOWA W DŁUGOŁĘCE, longitude=22.8683732}, timestamp=2023-05-19 23:56:45}
@JulWas797 JSONArray.toString() will output a JSON doc, if that is what you want. We cannot change toList() due to backwards compatibility. Can you describe what you are trying to do?
@JulWas797 JSONArray.toString() will output a JSON doc, if that is what you want. We cannot change toList() due to backwards compatibility. Can you describe what you are trying to do?
Thanks for responding!
What I'm trying to basically acheive is to access elements of JSONArray using Streams API, and using .toList() is the only option to do so.
As JSONArray already has Iterator, i would propose to integrate Streams directly in it (I will also check what I can do, and maybe try to make PR with this).
@stleary
I can propose a solution for this: I can make the org.json.JSONArray to implement the java.util.List interface and not the Iterable directly. This way, it will have the stream() method also all the functionality that a List should have. Because as I see from the class code it behaves like a List but with some minor changes.
(I have these changes ready for PR)
@stleary So if we can't use (for now at least) the Java 8 features we can't make the JSONArray return a Stream. The problem from the issue description is that when the toList() is called, it will translate all the JSONObject to HasMap and the HasMap class has the toString() method to print the map to this format.
@JulWas797 As I see, the only way to iterate the "real" data inside the JSONArray is from the Iterator. To make an Iterable to Stream you can do the following:
@ZachsCoffee It was probably not a great idea to include JSONArray.toList() in the project in the first place. The best thing to do now may be to mark it as @deprecated with a comment explaining how someone might get the desired functionality by coding an external method with the streaming API. You can add a getAsList(), optList(), or a similar method if you can think of a valid use case and code it in a way that does not require Java 8.
Comment from @aghasemi in #1012
It's obviously deliberate and the desired behaviour, and the reason is very likely somewhere documented, but unfortunately I could not find it. So may I ask what is the logic or use cases for this behaviour?
And will you consider a new method to return a list of objects without converting JSONObjects to Maps etc?
Just to make a note here: The proper way to use the Streams API on an object that exposes an iterator, but not a stream directly is to use the StreamSupport and Spliterators helper classes to make a Spliterator from the iterator, which you can them stream.
Activity
[-](JSONArray).toList() corrupts entries [/-][+](JSONArray).toList() breaks format of entries [/+]JulWas797 commentedon May 20, 2023
I decided to change the title, as it's late for me and i made mistake in it.
According to your javadocs, this is not a desired behavior.
stleary commentedon May 20, 2023
@JulWas797 JSONArray.toString() will output a JSON doc, if that is what you want. We cannot change toList() due to backwards compatibility. Can you describe what you are trying to do?
JulWas797 commentedon May 20, 2023
Thanks for responding!
What I'm trying to basically acheive is to access elements of
JSONArrayusing Streams API, and using.toList()is the only option to do so.As
JSONArrayalready has Iterator, i would propose to integrate Streams directly in it (I will also check what I can do, and maybe try to make PR with this).stleary commentedon Sep 4, 2023
Streams support proposed implementation: #768
aryanttripathi commentedon Oct 8, 2023
Sir @stleary , What will be the expected result we want? I just started putting my steps in the open-source community. I will be glad if I can help :)
ZachsCoffee commentedon Oct 8, 2023
@stleary
I can propose a solution for this: I can make the
org.json.JSONArrayto implement thejava.util.Listinterface and not theIterabledirectly. This way, it will have thestream()method also all the functionality that aListshould have. Because as I see from the class code it behaves like aListbut with some minor changes.(I have these changes ready for PR)
Priyanshu-ai902 commentedon Oct 16, 2023
Hi @stleary i am interested in this issue
Will you assign me this issue to work on it
stleary commentedon Oct 16, 2023
@Priyanshu-ai902 This project does not assign issues. Feel free to work on any issue that interests you.
ZachsCoffee commentedon Oct 22, 2023
@stleary So if we can't use (for now at least) the Java 8 features we can't make the
JSONArrayreturn aStream. The problem from the issue description is that when thetoList()is called, it will translate all theJSONObjecttoHasMapand theHasMapclass has thetoString()method to print the map to this format.@JulWas797 As I see, the only way to iterate the "real" data inside the
JSONArrayis from theIterator. To make anIterabletoStreamyou can do the following:stleary commentedon Oct 22, 2023
@ZachsCoffee It was probably not a great idea to include
JSONArray.toList()in the project in the first place. The best thing to do now may be to mark it as@deprecatedwith a comment explaining how someone might get the desired functionality by coding an external method with the streaming API. You can add agetAsList(),optList(), or a similar method if you can think of a valid use case and code it in a way that does not require Java 8.stleary commentedon Sep 20, 2025
Comment from @aghasemi in #1012
It's obviously deliberate and the desired behaviour, and the reason is very likely somewhere documented, but unfortunately I could not find it. So may I ask what is the logic or use cases for this behaviour?
And will you consider a new method to return a list of objects without converting JSONObjects to Maps etc?
Thanks
Best
johnjaylward commentedon Sep 22, 2025
Just to make a note here: The proper way to use the Streams API on an object that exposes an iterator, but not a stream directly is to use the StreamSupport and Spliterators helper classes to make a Spliterator from the iterator, which you can them stream.