0
public class X {
    Header property1;
    NeedHelp property2;
    Sections property3;
} 

Example JSON

{
header: {
name: "Test",
description: "Description"
},
needHelp: {
deckNumber: "deck Number"
},
sections: 
{
media: {
type: "image"
}
}
}

Expected JSON

{
header: {
name: "Test",
description: "Description"
},
needHelp: {
deckNumber: "deck Number"
},
sections: 
{
media: {
type: "image"
}
}

OR

{
header: {
name: "Test",
description: "Description"
},
sections: 
{
media: {
type: "image"
}
},
needHelp: {
deckNumber: "deck Number"
}

OR

{   
    needHelp: {
    deckNumber: "deck Number"
    },
    header: {
    name: "Test",
    description: "Description"
    },
    sections: 
    {
    media: {
    type: "image"
    }
    }
    }

Actual Response

{
    header: {
    name: "Test",
    description: "Description"
    },
    needHelp: {
    deckNumber: "deck Number"
    },
    sections: 
    {
    media: {
    type: "image"
    }
    }
    }

I tried to look at the @JsonPropertyOrder, but that is more for static order. When I convert this POJO to JSON, it should preserve the properties order.. ie One time I can receive property2, property3, property1 the next time I get back property1, property2, property3.

The final JSON should have that order preserved.

  • Why do you need to maintain a constant order of the json properties? I don't know of any json parser libraries which care about order, or specifically remember the original order of the fields as they are parsed. – joshmeranda Aug 23 '20 at 01:45
  • the order is a key .. there are some AEM components that could be rearranged in page.. and those components are mapped to this POJO and exposed as json. – codinginprogress Aug 23 '20 at 01:53
  • Can you add a small reproduction of the json in the question? – joshmeranda Aug 23 '20 at 02:01
  • Are those components rearranged entirely based on their ordering or are there other fields you can use to specify where they should be rendered (column, margin, etc), when a parent-child relationship cannot be established? – joshmeranda Aug 23 '20 at 02:02
  • Not really .. right now we dont have any property which saves the order number of it – codinginprogress Aug 23 '20 at 02:15
  • I edited your example a little to change it into something which may compile since your previous example couldn't (lets not introduce in examples new problems unrelated to one which you want to ask about). – Pshemo Aug 23 '20 at 02:22
  • Is the amount of components constant? If not, representing it in a POJO isn't really the right route to go down (douhbly so if order is both imperative and dynamic) but given the maintenance cost. Could you add an example of the json so we have a better idea of what we are dealing with? – joshmeranda Aug 23 '20 at 02:28
  • @puffin Example json the header , needHelp, section type can be in any order `{ header: { name: "Test", description: "Description" }, needHelp: { deckNumber: "deck Number", }, }, sections: { media: { type: "image" } }` – codinginprogress Aug 23 '20 at 02:40
  • Please use [edit] option when you try to clarify your question. Not many people will be willing to read all comments to search for crucial info (since it should be in the question itself). Also aside from input provide expected result. – Pshemo Aug 23 '20 at 02:42
  • @Pshemo added it in the question .. – codinginprogress Aug 23 '20 at 02:47
  • You may have a look to SO link https://stackoverflow.com/a/4516397/13878023 – Sujitmohanty30 Aug 23 '20 at 04:45
  • Does this answer your question? [Keep the order of the JSON keys during JSON conversion to CSV](https://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv) – Zaid Warsi Aug 23 '20 at 09:04

0 Answers0