0

I have the need to send to my back-end a JSON recursive array which contains several sorted objects and arrays inside.

From what I've read JSON doesn't guarantee that Json Objects are ordered, in fact my issue is that if I go in debug on the java endpoint, the array received randomly lose the order of "subchildren" array .

Although I'm not completely sure that is the problem because in my case I have an Array which contains an object which contains another array and it's this last array which lose the order not his parent object

How can I successfully send and receive between back-end and front-end this kind of structure (always created in the react front-end application) ?

E.g. this sample is sent to a post http://localhost:8080/dynasty/1 but the subchildren array lose it's order when received by the java endpoint (sometimes there is first the child2 or child3 which isn't correct)

    dynasty_tree : [
     {
      name: father,
      age: 50,
      subchildren: [
        {
            name: child1,
            age: 30,
            subchildren: []
        },
        {
            name: child2,
            age: 28,
            subchildren: []
        },
        {
            name: child3,
            age: 26,
            subchildren: []
        },
      ]
    }
  ]

the data from front-end is sent with axios:

const handleSubmitForm = async (e) => {
 const result = await axios.post( http://localhost:8080/dynasty/${id},  data );
}

java back-end method:

@PostMapping("/dynasty/{id}")
public ResponseEntity<?> savePeopleById(@RequestBody Iterable<person> peopleList, @PathVariable int id,  BindingResult result){

thank you for the help

user2298581
  • 532
  • 3
  • 11
  • 37
  • can you show how you are sending your post request to the server? or check if the array is really sorted – T.sagiv Nov 25 '20 at 17:05
  • The array in front-end is sorted I made sure of it. The problem is as said here https://stackoverflow.com/questions/7214293/is-the-order-of-elements-in-a-json-list-preserved#:~:text=Yes%2C%20the%20order%20of%20elements,null%2C%20object%2C%20or%20array. order of array inside object are not guaranted. The object is sent like this: const result = await axios.post( `http://localhost:8080/dynasty/${id}`, data ); – user2298581 Nov 26 '20 at 08:57
  • So it seems like your backend is doing the trouble. – T.sagiv Nov 26 '20 at 12:19
  • JSON objects aren't ordered by definition is not a back-end trouble ...I guess there must be workaround which I'd like to know ... – user2298581 Nov 26 '20 at 14:00
  • Well I don't know he says that the arrays are ordered before he sends it. I don't see any reason why it should be shuffeld – T.sagiv Nov 26 '20 at 15:37

0 Answers0