Better JSON representation for GET ALL REST API response - json

What is the better JSON representation for GET ALL REST API response? With Option 1, it is easier to get the details of any particular employee. With Option 2, we need to filter the complete array to get any particular employee details. So, Option 1 seems to be better option. Are there any other scenarios where Option 2 can be better?
Option 1:
{
"data":{
"1234":{
"name":"ABC",
"country":"US",
"mobile":"999999999"
},
"1235":{
"name":"BDE",
"country":"IND",
"mobile":"999999998"
}
}
}
Option 2:
{
"data":[
{
"empId":"1234",
"name":"ABC",
"country":"US",
"mobile":"999999999"
},
{
"empId":"1235",
"name":"BDE",
"country":"IND",
"mobile":"999999998"
}
]
}

For option 1, since data is a JSON object, the order is not preserved and you cannot iterate over it in order. This may or may not be an issue depending on your use case. If this data is being consumed by a front-end app and you want to display the employees in order, then option 2 would be more suitable. If ordering doesn't matter, go with option 1.

Related

JSON structure for message payload

I am sending a message to a third party and the payload looks like as below:
{
"keys": {
"salesCaseId": 1000449
},
"attributes": {
"assetDeliveryDetails": {
"registrationNumber": "PXS3388",
"vinNumber": "DW1265",
"deliveryDate": "16-04-2020"
}
}
}
"Keys" and "attributes" are common for all the messages between us.
Question: Because I have only one nested JSON inside attributes which is assetDeliveryDetails is it necessary to have this nested JSON property? or I can have the other 3 attributes one level higher as below :
{
"keys": {
"salesCaseId": 1000449
},
"attributes": {
"assetRegistrationNumber": "PXS3388",
"assetVinNumber": "DW1265",
"assetDeliveryDate": "16-04-2020"
}
}
Does it makes sense to group because I can logically group them together or is it an overhead because I need to create equivalent DTO classes as well?
Generally speaking you want any data structure (whether that's JSON, XML, an Object model, etc.) to be as simple as possible to fulfill your requirements. If the second model (i.e. without assetDeliveryDetails) fulfills your requirements then I recommend you go with that. Unnecessary complexity is not desirable as it will be more difficult to understand and take more resources to parse..

Is it okay to use a flexible json attribute type? array vs object based on the amount of items?

i'm trying to integrate our application with a payment providers. There API uses both Arrays and Objects on the same json property.
Example:
When getting the Shoppingcart with cart-items, the response will be like this when there is one cart-item:
GET /cart/{cart-identifier}
{
"cart_identifier": 1,
"items": {
"product_identifier": 2,
"amount": 1
}
}
When there are 2 items in the cart, the response will be like this.
{
"cart_identifier": 1,
"items": [
{
"product_identifier": 2,
"amount": 1
},
{
"product_identifier": 3,
"amount": 1
}
]
}
To me, this does not make sense, but does anyone know what the JSON specification says of this? And are there any good reasons to do it this way?
Ps: If you have some good blog posts that are related to this then please send me a message.
As far as the JSON specification is concerned, both forms are correct. You may have problems if you try to map the JSON to a Java class though. This depends on what parser you're using.

JSON Deserialization on Talend

Trying to figuring out how to deserialize this kind of json in talend components :
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
"ryan.buckley#toofr.com": {
"confidence":18,"email":"ryan.buckley#toofr.com","default":16
},
"ryanbuckley#toofr.com": {
"confidence":17,"email":"ryanbuckley#toofr.com","default":17
},
"ryan_buckley#toofr.com": {
"confidence":16,"email":"ryan_buckley#toofr.com","default":18
},
"ryan-buckley#toofr.com": {
"confidence":15,"email":"ryan-buckley#toofr.com","default":19
},
"ryanb#toofr.com": {
"confidence":14,"email":"ryanb#toofr.com","default":14
},
"buckley#toofr.com": {
"confidence":13,"email":"buckley#toofr.com","default":13
}
}
This JSON comes from the Toofr API where documentation can be found here .
Here the actual sitation :
For each line retreived in the database, I call the API and I got this (the first name, the last name and the company change everytime.
Does anyone know how to modify the tExtractJSONField (or use smthing else) to show the results in tLogRow (for each line in the database) ?
Thank you in advance !
EDIT 1:
Here's my tExtractJSONfields :
When using tExtractJSONFields with XPath, you need
1) a valid XPath loop point
2) valid XPath mapping to your structure relative to the loop path
Also, when using XPath with Talend, every value needs a key. The key cannot change if you want to loop over it. Meaning this is invalid:
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
but this structure would be valid:
{
"contact": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"contact": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
So with the correct data the loop point might be /contact.
Then the mapping for Confidence would be confidence (the name from the JSON), the mapping for Email would be email and vice versa for default.
EDIT
JSONPath has a few disadvantages, one of them being you cannot go higher up in the hierarchy. You can try finding out the correct query with jsonpath.com
The loop expression could be $.*. I am not sure if that will satisfy your need, though - it has been a while since I've been using JSONPath in Talend because of the downsides.
I have been ingesting some complex json structures and did this via minimal json libraries, and tjava components within talend.

Solr Mulivalued Problem

Consider The following is the json response i'm getting from the solr if i use multivalued = true for the fields.
{
"id":["1","2","3"],
"TS":["2010-06-28 00:00:00.0","2010-06-28 00:00:00.0","2010-06-28 00:00:00.0"],
"Type":["VIDEO","IMAGE","VIDEO"]
}
but i need the response like this
{
"0":["1","2010-06-28 00:00:00.0","VIDEO"],
"1":["2","2010-06-28 00:00:00.0","IMAGE"],
"2":["3","2010-06-28 00:00:00.0","VIDEO"]
}
How can i get this.Any help would be appreciated. Thanks in advance.
**Update :**
Actually at the first level its not a problem. When we are going
more than one level then only the
problem arises. right now i'm putting
the entire response here to make it
clear.
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"facet":"true",
"indent":"on",
"start":"0",
"q":"laptop",
"wt":["json",
"json"],
"rows":"200"}},
"response":{"numFound":1,"start":0,"docs":[
{
"createdBy":"0",
"id":194,
"status":"ACTIVE",
"text":"Can i buy Sony laptop?",
"ansTS":["2010-07-01 00:00:00.0","2010-08-06 15:11:55.0","2010-08-11 15:28:13.0","2010-08-11 15:30:49.0","2010-08-12 01:45:48.0","2010-08-12 01:46:18.0"],
"mediaType":["VIDEO","VIDEO","VIDEO"],
"ansId":["59","76","77","78","80","81"],
"mediaId":[24,25,26],
]},
]
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"catName":[]},
"facet_dates":{}}}
look at the mediaId , mediatype ,ansTS arrays. Its one to many relationship.But they are grouped by column names.Thanks in advance.
You mentioned that you will consume this JSON from a browser. So you can use jQuery or any other javascript library to convert the raw Solr JSON response into the structure that you need.
If the first snippet is the actual solr response you're getting, then chances are you have a bug in your feeder (connector/crawler/etc). It looks like you only have one indexed document (that matches your query), which has all the values that you expect from 3 documents.
Assuming you have 3 documents, analogous with your expected output, then the actual solr wt=json result would contain:
[{
"id":"1",
"TS":"2010-06-28 00:00:00.0",
"Type":"VIDEO"
},
{
"id":"2",
"TS":"2010-06-28 00:00:00.0",
"Type":"IMAGE"
},
{
"id":"3",
"TS":"2010-06-28 00:00:00.0",
"Type":"VIDEO"
}]
If this assumption is correct, then I would suggest looking over your indexing logic.
This output is produced by Solr's JSONResponseWriter. Its output can't be altered via configuration. But what you can do is create your own version of JSONResponseWriter to produce your desired output. You can registered your new ResponseWriter by adding a queryResponseWriter tag in solrconfig.xml.

JSON structuring a groupchat

I'm going to implement a groupchat into my app by using Firebase. I were thinking of 2 different structures of saving data in JSON.
First structure:
Second structure:
I would like to achieve a fast query and i would like it to parse a small amount of data each time. What structure should i go for and are there maybe a better alternative then these 2?
The first solution is clearly not viable, as you would have a hard time finding all messages belonging to a given group.
The second solution is ok if every time you will query a given group's node, you are going to need also all of its messages, which is probably not what you want.
It is hard, of course, to advise on data structure without more info on your use-case, the queries you are going to make, etc., but a rather standard approach would be:
{
"users": {
"$userId"": {
// user data
}
},
"groups": {
"$groupId": {
// group data
}
},
"group_users": {
"$groupId": {
"$userId": true
// separation of list of users from the group is useful
// if you are going to query the group node not needing its full list of users
}
},
"group_messages": {
"$groupId": {
"$messageId": {
// message data
}
}
}
}