Is this JSON response good to generate?

I’ve the following table(with data) in the database from where I am planning to generate a JSON response using Java webservice.

DATAID DESC DATADESC
1 Capital, Summary etc Counts of capital and summary
2 Capital, Summary etc Testing of capital and summary
3 First Reports Completed Successfully
4 First Reports Still Pending
5 First Reports Failed twice
6 First Reports Re attempting …

I need to display in the UI in the following format:

As you can see first two DESC are same and same is true with last 4 as well, so I want to combine it in the following manner(basically in the form of a heading as shown below).

Here’s how it’s going to look in the UI :

Capital,Summary etc

  • Counts of capital and summary
  • Testing of capital and summary

First Reports

  • Completed Successfully
  • Still Pending
  • Failed twice
  • Re attempting …|

A simple way I was thinking of generating the JSON response is :

{
	"webservice_status": {
		"status": "SUCCESS",
		"message": ""
	},
	"dataInfo": [{
			"desc": "Capital,Summary etc",
			"datadesc": "Counts of capital and summary "

		},
		{
			"desc": "Capital,Summary etc",
			"datadesc": "Testing of capital and summary "
		},
		{
			"desc": "Capital,Summary etc",
			"datadesc": "Testing of capital and summary "
		},
		{
			"desc": "First Reports",
			"datadesc": "Completed Successfully "
		},
		{
			"desc": "First Reports",
			"datadesc": " Still Pending "
		},
		{
			"desc": "First Reports",
			"datadesc": "Failed twice "
		},
		{
			"desc": "First Reports",
			"datadesc": "Re attempting ... "
		}
	]
}

Could anyone tell me if this is a correct way of generating the response based on the information in the database available OR should I consider modifying it in some manner such that the duplicate desc should not be shown twice but their contents(datadesc)?

Also, I am wondering, if I go ahead with the above JSON response, how would I know that I need to stop at the second desc which would be the first heading and then consider the following desc (which is First Reports) for second heading. Here also, how would I know that I need to stop at 4th step and combine all the desc into one?

I think it makes sense given the data. But I don’t see a reason to have duplicate entries, there may very well be a good reason, I just don’t see it.

There are a few standards for producing JSON structures, but personally I’m not a fan of many and prefer to give simple structures that make the most sense for the given data.

Also, I am wondering, if I go ahead with the above JSON response, how would I know that I need to stop at the second desc which would be the first heading and then consider the following desc (which is First Reports ) for second heading. Here also, how would I know that I need to stop at 4th step and combine all the desc into one?

I’m not sure what you’re wanting to do here. Can you explain it a different way?

As per my understanding, the DATADESC column has unique entries in each cell. However,DESC column has duplicate entries because multiple cell values in the DATADESC column belongs to same DESC cell value. The database guy might have not designed it properly I believe. I may be wrong. I can ask him. Do you thin it should be designed in some different manner? I can ask the database guy. Based on my understanding, having multiple entries in a column is a violation of some normal form (don’t remember 1st NF or 2nd NF etc)

Why is there no dataid in the JSON?

I can include that, that’s not a problem.Is having dataid in the JSON response going to do anything better? Thanks !

Well, it’s just that I’m not really sure how you were going about showing it when it wasn’t in the JSON :slightly_smiling_face:

Ok… So you mean, if I include it, then I should be able to do achieve like this in the UI ?

image

I was wondering, how would I stop at DATAID=2 at the time of displaying it at UI? Because, from DATAID=3 onwards, I have a new DESC value which goes until DATAID=6. I would again have to stop at DATAID=6 to consolidate the value of DESC from 3 to 6 into one heading in the UI.

That JSON looks fine to me… another way of representing one to many relations might be something like this

[
  {
    "desc": "Capital,Summary etc",
    "datadesc": [
      "Counts of capital and summary ",
      "Testing of capital and summary ",
      "Testing of capital and summary "
    ]
  },
  {
    "desc": "First Reports",
    "datadesc": [
      "Completed Successfully ",
      " Still Pending ",
      "Failed twice ",
      "Re attempting ... "
    ]
  }
]

… but personally I’d rather keep the JSON response to be as simple as possible, so it’s easier to extend with additional relations (say). And you can still convert it to a more dedicated structure (like the above) on the client side where required, for example like so:

const data = json.dataInfo.reduce((result, current) => {
  const entry = result.find(({ desc }) => current.desc === desc)
  const { desc, datadesc } = current

  if (entry) {
    entry.datadesc.push(datadesc)
  } else {
    result.push({ desc, datadesc: [datadesc] })
  }

  return result
}, [])

Thanks. This approach sounds great to me. I can preserve my original JSON response as well in case I need to do any other changes in future.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.