Is there a way to deserialize a JSON that includes references to objects that already exist inside it using typescript?
For example we have a grand parent "Papa" that is associated with two parents "Dad" and "Mom" that they have together two children, the json looks like:
{
"id_": 1,
"name": "Papa",
"parents": [
{
"#class": "com.doubleip.spot.mgmt.test.domain.model.Parent",
"id_": 1,
"name": "Dad",
"children": [
{
"#class": "com.doubleip.spot.mgmt.test.domain.model.Child",
"id_": 1,
"name": "Bob"
},
{
"#class": "com.doubleip.spot.mgmt.test.domain.model.Child",
"id_": 2,
"name": "Trudy"
}
]
},
{
"#class": "com.doubleip.spot.mgmt.test.domain.model.Parent",
"id_": 2,
"name": "Mom",
"children": [
1,
2
]
}
]
}
You may see that the children of Mom are just inserted as the value of their "id_" field. This happens due to JsonIdentityInfo used in Java and fasterxml library.
So we face problem in front-end deserialisation where we use typescript angular and primeng in order to visualise our data.
So we face problem in front-end deserialisation
you need to write most of the code yourself (or generate it using more code from your Java code).
That said, there are a few hydration helpers. I recommend : https://github.com/mobxjs/serializr
Related
When returning a list of objects in a JSON response, say a GET request to a /movies endpoint, is it more common to return a JSON array or an object that wraps a JSON array? I've seen both formats in APIs and I was wondering if the standard. If there isn't, which way is preferable?
i.e.
[
{
"name": "Harry Potter",
"year": 2000
}
]
vs.
{
"movies": [
{
"name": "Harry Potter",
"year": 2000
}
]
}
In general if you have a service that only return a list, the first option is perfect fine:
[
{
"name": "Harry Potter",
"year": 2000
}
]
But if you are thinking in a general way to do it will be better add more context data, as total items counter, pagination variables or status values. So in spite of the first one is perfectly fine, I always prefer the second one, but without the name of the collection/array/table name and with more context info, as for example:
{
"items": [
{
"name": "Harry Potter",
"year": 2000
}
],
"total": 1,
"page": 1,
"pages": 1
"status": 1,
"timestamp: 121344
}
Set the array nested on movies value is a bit redundant. But for my it's only a practical approach that for my experience is more readable and used in all projects which I am related.
I'm aware of this question but I don't believe that there is no solution with standars component. I'm using Talend ESB Studio 5.4.
I have to parse a JSON string from a REST web service into multiple output, and add them to a database.
Database has two tables:
User (user_id, name, card, card_id, points)
Action (user_id, action_id, description, used_point)
My JSON Structure is something like that:
{
"users": [
{
"name": "foo",
"user_id": 1,
"card": {
"card_id": "AAA",
"points": 10
},
"actions": [
{
"action_id": 1,
"description": "buy",
"used_points": 2
},
{
"action_id": 3,
"description": "buy",
"used_points": 1
}
]
},
{
"name": "bar",
"user_id": 2,
"card": {
"card_id": "BBB",
"points": -1
},
"actions": [
{
"id": 2,
"description": "sell",
"used_point": 5
}
]
}
]
}
I have tried to add a JSON Schema Metadata but it is not clear to me how to "flat" the JSON. I have tried to look at tXMLMap, tExtractJSONFields.. but no luck till now.
I also had a look at tJavaRow but I don't understand how to make a Schema for that.
It's a pity because till now I'm loving Talend! Any advice?
You can save a json file in your disk, then create new json file in the metadata of Talend studio, the wizard retrieve the schema for you, after saving, you ca, copie schema in the generic schema of the metadata, and it's done, use that generic schema where you want, this is how to use generic schema in the tRestClient component:
Please help me in parsing this Json sample as I'm not able to parse it because of the complexity of it as well as different objects inside it. I'm able to parse Json when a list of same objects & same structure but not like the one below.
[
{
"notificationBrowserHead":
{
"notificationId": 4,
"notificationType": "NEW_PRODUCT",
"creationTime": 1421933381000,
"notificationNormalUserId": 4,
"notificationViewed": false
},
"brandIdAndNameHolder":
{
"brandId": 1,
"name": "B1"
},
"brandLogo": null,
"productIdAndNameHolder":
{
"productId": 1,
"name": "JK product1"
}
},
{
"notificationBrowserHead":
{
"notificationId": 2,
"notificationType": "USER_INT_COMMENT",
"creationTime": 1421924403000,
"notificationNormalUserId": 2,
"notificationViewed": false
},
"uploadId": 22,
"uploadThumbnail": "/mediaUrl/location/thumbNail",
"uploadDescription": "upload 1 location desc",
"notificationCreator":
{
"normalUserId": 90,
"displayName": "amit"
},
"uploadRemoved": false
},
{
"notificationBrowserHead":
{
"notificationId": 1,
"notificationType": "NEW_LOCATION_VOTE",
"creationTime": 1421924403000,
"notificationNormalUserId": 1,
"notificationViewed": false
},
"locationIdAndNameHolder":
{
"locationId": 11,
"name": "Current King JK"
},
"locationLogo": null
}
]
Any help would be truly appreciated.
I presume that you receive different set of json properties when your NotificationType varies.
Solution 1:
Define all your members(the collection of all your properties that you receive for different types of notification) in a Class and use it for DeSerialization, so that the unwanted properties for your particular notification type will be null.
Solution 2:
Parser manually. Newtonsoft json documentation here
Make class "Notifications (or something)" and put inside everything you got back from json2csharp.com site, then use this framework http://www.newtonsoft.com/json to deserialize data as you download it from server and you should be able to get notificationType by Object.Notificationbrowserhead[x].notificationType or similar.
How does bind values of embedded JSON data to input elements in EmberJS as I cannot seem to find a straight forward way to get that done.
update
It doesn't seem to work for a JSON object with this structure
{
"users": [
{
"_id": "534550428047526419000002",
"Name": "admin",
"Code": "admin",
"Age": 12,
"Sex": "Male",
"Ethnicity": "admin",
"EventTime": "",
"Accident": [
{
"value": true,
"risk": "Medium"
}
]
}
]
}
In the template you use the property name
{{foo}}
{{input value=foo}}
Example
http://emberjs.jsbin.com/wufegaca/1/edit
nested properties are handled the same way they are in most languages with the dot operator
{{foo.bar}}
{{input value=foo.bar}}
Example
http://emberjs.jsbin.com/wufegaca/2/edit
Using an array you need to iterate over the array. If you didn't how would Ember know which item you were referring to? Additionally upper case property names are problematic in handlebars, since it usually denotes a global namespace, so be wary if things don't appear to be working.
Example
http://emberjs.jsbin.com/wufegaca/3/edit
I have an ASP.NET MVC 4 Web API app using EntityFramework for ORM.
In the JSON I return, there are some cases where the same child node is present for multiple parent nodes. In these cases, the first occurrence of the child node is fully visible with all it's members. Any subsequent occurrence shows up as a $ref to the first occurrence.
I'd like instead to see the full object everytime it shows up in the JSON returned.
For example, instead of seeing:
[{
"$id": "1",
"userId": 1,
"Badge": {
"$id": "2",
"badgeId": 1,
"badgeName": "Gold"
}
}, {
"$id": "3",
"userId": 2,
"Badge": {
"$ref": "2"
}
}]
i'd like to see:
[{
"$id": "1",
"userId": 1,
"Badge": {
"$id": "2",
"badgeId": 1,
"badgeName": "Gold"
}
}, {
"$id": "3",
"userId": 2,
"Badge": {
"$id": "4",
"badgeId": 1,
"badgeName": "Gold"
}
}]
Basically I want to get rid of any "$ref" in the JSON. Is there a way?
Thanks!
An easy way is to edit the generated entity classes code. For each of the entity classes, there will be a [DataContract(IsReference=true)] attribute assigned.
Something like the following:
[EdmEntityTypeAttribute(NamespaceName="YourNamespace", Name="YourEntity")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class YourEntity : EntityObject
{
Change it to IsReference=false. That should do the trick.
In my case, I'm using the Entity model, I simply set an entity key for a unique field in my .edmx diagram table.