How to parse JSON to List with Maps in Gson? - json

I have the following Java Classes:
public class ModuleParsed {
String id_component;
String id_instance;
Map<ModuleParam, ModuleParam> input;
Map<ModuleParam, List<ModuleParam>> output;
int id_paas;
}
and
public class ModuleParam {
String name;
Object type;
}
what should be the JSON expected to parse it as a List<ModuleParsed>?
listModules = gson.fromJson(br, new TypeToken<List<ModuleParsed>>() {}.getType());
Everything was ok until I introduced the input and output parameters.
EDIT
By doing the reverse process I have found the JSON should be something like
[
{
"id_component": "mod1",
"id_instance": "mod1_inst1",
"input": {
"moduleParam": {
"name": "param3",
"type": "obj3"
},
"moduleParam": {
"name": "param2",
"type": "obj2"
}
},
"id_paas": 1
},
{
"id_component": "mod2",
"id_instance": "mod2_inst1",
"input": {
"moduleParam": {
"name": "param3",
"type": "obj3"
},
"moduleParam": {
"name": "param2",
"type": "obj2"
}
},
"id_paas": 1
}
]
where the moduleParam elements were something like: parser.ModuleParam#36c51089
No matter how I name these elements I get the following error:
Exception in thread "main" com.google.gson.JsonParseException: Expecting object found: "moduleParam"
How can I obtain the class ModuleParsed with Gson?

Following #Brian's comment I realized I had no need to use complex objects as keys so this was my solution.
public class ModuleParsed {
String id_component;
String id_instance;
Map<String, ModuleParam> input;
Map<String, List<ModuleParam>> output;
int id_paas;
}
public class ModuleParam {
String name;
String id_module;
String id_instance;
}
for which I built the following JSON
[
{
"id_component": "mod1",
"id_instance": "mod1_inst1",
"input": {
"input1": {}
},
"output": {
"output1": [
{
"name": "input1",
"id_module": "mod2",
"id_instance": "mod2_inst1",
"type": "paramType"
},
{
"name": "input2",
"id_module": "mod2",
"id_instance": "mod2_inst1",
"type": "paramType"
}
],
"output2": []
},
"id_paas": 1
},
{
"id_component": "mod2",
"id_instance": "mod2_inst1",
"input": {
"input1": {
"name": "output1",
"id_module": "mod1",
"id_instance": "mod1_inst1",
"type": "paramType"
},
"input2": {
"name": "output1",
"id_module": "mod1",
"id_instance": "mod1_inst1",
"type": "paramType"
}
},
"output": {},
"id_paas": 1
}
]
The JSON was parsed correctly this time.

Related

Jackson Serialize Enum as Object

i've a enum in my json-schema, which looks like this:
"definitions": {
"parentObject": {
"type": "object",
"properties": {
"child": {
"$ref": "#/definitions/childType"
}
}
},
"childType": {
"type": "string",
"enum": [
"BROTHER",
"SISTER",
"DAUGTHER"
]
}
}
I need to implement it as a java-enum.
#JsonTypeName("parentObject")
public class FahrtSegmentUtilizationType {
#JsonProperty(value = "child")
private childType child;
}
and i've tried out a lot of jackson-annotations, but nothing helps.
#JsonTypeName("childType")
//#JsonFormat(shape = JsonFormat.Shape.OBJECT)
//#JsonSerialize(using = ChildTypeSerializer.class)
public enum ChildType {
BROTHER("BROTHER"),
SISTER("SISTER"),
DAUGHTER("DAUGTHER")
private String child;
#JsonCreator
ChildType(String type) {
this.child = type;
}
//#JsonValue
public String getUtilazation() {
return this.child;
}
}
Everytime i got
"parentObject": {
"type": "object",
"properties": {
"child": {
"type" : "string",
"enum" : [
[
"BROTHER",
"SISTER",
"DAUGTHER"
]
]
}
}
}
With "#JsonFormat(shape = JsonFormat.Shape.OBJECT)" i got $ref on ChildType-Object, but this referenz-object stays empty.
How can i implement enum, so it looks like in wished schema?

Parsing Iterative JSON from Firebase to Flutter

I have data saved into Realtime Firebase as an iterative JSON as shown in the picture.
Realtime Firebase data
[
{
"name": "Math",
"subMenu": [
{
"name": "Math1",
"subMenu": [
{
"name": "Math 1.1"
},
{
"name": "Math 1.2",
"subMenu": [
{
"name": "Math 1.2.1",
"subMenu": [
{
"name": "Math 1.2.1.1"
},
{
"name": "Math 1.2.1.2"
}
]
},
{
"name": "Math 1.2.2"
}
]
}
]
},
{
"name": "Math2"
},
{
"name": "Math3",
"subMenu": [
{
"name": "Math 1.3.1"
},
{
"name": "Math 1.3.2"
}
]
}
]
},
{
"name": "Marketing",
"subMenu": [
{
"name": "Promotions",
"subMenu": [
{
"name": "Catalog Price Rule"
},
{
"name": "Cart Price Rules"
}
]
},
{
"name": "Communications",
"subMenu": [
{
"name": "Newsletter Subscribers"
}
]
}
]
}
]
How the JSON look like in Realtime Firebase
'Click the image'
datamodel.dart
class Menu {
String? name;
int? font;
List<Menu>? subMenu = [];
Menu({this.name, this.subMenu, this.font});
Menu.fromJson(Map<String, dynamic> json) {
font = json['font'];
name = json['name'];
if (json['subMenu'] != null) {
json['subMenu'].forEach((v) {
subMenu?.add(Menu.fromJson(v));
});
}
}
}
My goal is to build a multilevel list view in Flutter that reflexes iterative JSON structure. So, I implemented a method that returns List<Menu>, and then pass it to a Futurebuilder to build a multilevel list View.
The method.
final ref = FirebaseDatabase.instance.ref();
Future<List<Menu>> firebaseCalls(DatabaseReference ref) async {
final snapshot = await ref.child('Task').get();
final jsondata = snapshot.value as Map<String, dynamic>;
final list = json.decode(jsondata) as List<dynamic>; // Error Location
return list.map((e) => Menu.fromJson(e)).toList();
}
and I got the following
The Error
error: The argument type 'Map<String, dynamic>' can't be assigned to the parameter type 'String'. (argument_type_not_assignable at [flutter_multilevel_list_from_json] lib\main.dart:28)
tried to change the list type to List<dynamic> but still give me an error.
json.decode() takes a String as input, and you are passing a Map<String,dynamic> into it.
That is your problem, not that you are trying to cast it to a List<dynamic>
May be this will be helpful (jsondata as List).map((e) => Menu.fromJson(e)).toList();

NewtonSoft JSON does not parse referenced items in an array

I writing an web API which excepts JSON with references, I am using JsonPropertyAttribute and JsonObjectAttribute to mark properties/classes which can be refereed by some other property.
when i am receiving data from API it is working as expected, having corresponding $id and $ref property set in JSON. But when I post same JSON date to API it is not parsing all data back. Bellow is data I am receiving from API and same I am posting back.
{
"GeneralDetails": {
"Description": "23",
"ScheduleInfo": {
"ScheduleFrequency": 701,
"ScheduledDay": null,
"ScheduledMonth": null,
"ScheduledTime": "2018-04-10T21:30:01"
},
"ImagePath": "",
"BrandId": 23,
"Name": "23",
"Id": 29
},
"TargetDetails": {
"Period": 0,
"Type": 2701,
"Targets": [
{
"TierId": 34,
"Value": 29
}
]
},
"PointsDetails": [
{
"BooleanExperession": {
"$id": "1",
"$type": "Payback.TradeTool.Entities.Model.Offer.OfferRuleBooleanExpression, Payback.TradeTool.Entities",
"Variable1": {
"$id": "2",
"$type": "Payback.TradeTool.Entities.Model.Offer.OfferRulePreDefinedVariable, Payback.TradeTool.Entities",
"GroupId": 2,
"Type": 12,
"AvailableOperant": 289,
"Description": "Product SKU",
"Name": "Product SKU",
"Id": 67
},
"Variable2": {
"$id": "3",
"$type": "Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.String, mscorlib]], Payback.TradeTool.Entities",
"Value": "23",
"Type": 9,
"AvailableOperant": 289,
"Description": "2323",
"Name": "2323",
"Id": 132
},
"Operation": 1,
"Type": 130,
"AvailableOperant": 0,
"Description": "23",
"Name": "23",
"Id": 133
},
"Value": 23,
"Id": 16,
"Type": 0,
"ValueX": null
}
],
"Constants": [
{
"$id": "4",
"Value": "rty",
"Type": 9,
"AvailableOperant": 289,
"Description": "rty",
"Name": "yurt",
"Id": 130
},
{
"$id": "5",
"Value": 23,
"Type": 17,
"AvailableOperant": 231,
"Description": "23",
"Name": "ewe",
"Id": 131
},
{
"$ref": "3"
}
],
"BooleanExpressions": [
{
"$ref": "1"
}
],
}
when i am posting back Constants with $id 4 and 5 are not in parsed data. However if I refer them in some other property, then they are also parsing.
EDIT:
below is my class structure
public class OfferDetails
{
public OfferDetails()
{
GeneralDetails = new OfferGeneralDetails();
TargetDetails = new OfferTargetDetails();
PointsDetails = new List<OfferPointsDetails>();
Constants = new List<OfferRuleConstant>();
BooleanExpressions = new List<OfferRuleBooleanExpression>();
Variables = new Dictionary<string, List<OfferRulePreDefinedVariable>>();
}
public OfferGeneralDetails GeneralDetails { get; set; }
public OfferTargetDetails TargetDetails { get; set; }
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.All)]
public List<OfferPointsDetails> PointsDetails { get; set; }
[JsonProperty(IsReference = false, ItemTypeNameHandling = TypeNameHandling.All)]
public List<OfferRuleConstant> Constants { get; set; }
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.All,IsReference = false)]
public List<OfferRuleBooleanExpression> BooleanExpressions { get; set; }
public Dictionary<string, List<OfferRulePreDefinedVariable>> Variables { get; set; }
}
[JsonObject(ItemTypeNameHandling = TypeNameHandling.All, IsReference = true)]
public class OfferRuleConstant<T> : OfferRuleConstant
{
public OfferRuleConstant()
{
Type = RuleVariableType.Constant;
}
public T Value { get => (T)_value; set=> _value = value;}
public override string ToString()
{
return Value.ToString();
}
}
public abstract class OfferRuleConstant : OfferRuleVariable, ISeprateStringReprsentation
{
protected object _value;
public string StringReprsentation { get ; set ; }
}
[JsonObject(IsReference = true, ItemTypeNameHandling = TypeNameHandling.All)]
public class OfferRuleVariable : NamedEntity<long>
{
public OfferRuleVariable()
{
Type = RuleVariableType.Variable;
}
public RuleVariableType Type { get; set; }
public RuleOperantEnum AvailableOperant { get; set; }
public string Description { get; set; }
// public int GroupId { get; set; }
}
[JsonObject(IsReference = true, ItemTypeNameHandling = TypeNameHandling.All)]
public class OfferRulePreDefinedVariable : OfferRuleVariable
{
public int GroupId { get; set; }
}
public interface ISeprateStringReprsentation
{
string StringReprsentation { get; set; }
}
It is possible that I have made certain changes in model (Added a new Property, in a few class and also have changed some attribute while trying to fix above issue).
so, this is updated JSON I am trying to parse:
{
"$id":0,
"GeneralDetails":{
"$id":1,
"ScheduleInfo":{
"$id":2,
"ScheduleFrequency":701,
"ScheduledDay":null,
"ScheduledMonth":null,
"ScheduledTime":null,
"ScheduledTimeS":null
},
"Name":"tuy",
"Description":"ty"
},
"BooleanExpressions":[
{
"$id":3,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleBooleanExpression, Payback.TradeTool.Entities",
"Type":130,
"Variable1":{
"$id":4,
"Id":46,
"Name":"Transaction DateTime",
"Type":100,
"AvailableOperant":55,
"Description":"Transaction DateTime"
},
"Variable2":{
"$id":5,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.DateTime, mscorlib]], Payback.TradeTool.Entities",
"Type":97,
"Value":"2018-04-19T10:36:02.000Z",
"Name":"6",
"AvailableOperant":7,
"Description":"6",
"StringReprsentation":"4/19/2018, 4:06:02 PM"
},
"Operation":1,
"AvailableOperant":224,
"Name":"tuy",
"Description":"uy",
"StringReprsentation":"(Transaction DateTime EqualTo 4/19/2018, 4:06:02 PM)"
},
{
"$id":6,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleBooleanExpression, Payback.TradeTool.Entities",
"Type":130,
"Variable1":{
"$id":7,
"Id":48,
"Name":"Transaction Month",
"Type":4100,
"AvailableOperant":57,
"Description":"Transaction Month"
},
"Variable2":{
"$id":8,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleListConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":5649,
"Value":[
2,
4,
3
],
"Name":"13",
"AvailableOperant":40,
"Description":"33",
"StringReprsentation":"February,April,March"
},
"Operation":8,
"AvailableOperant":224,
"Name":"tyu",
"Description":"yu",
"StringReprsentation":"(Transaction Month In February,April,March)"
}
],
"TargetDetails":{
"$id":9,
"Period":701,
"Type":2701,
"Targets":[
{
"$id":10,
"TierId":34,
"Value":1
},
{
"$id":11,
"TierId":86,
"Value":2
},
{
"$id":12,
"TierId":87,
"Value":3
},
{
"$id":13,
"TierId":88,
"Value":4
}
]
},
"PointsDetails":[
{
"$id":14,
"BooleanExperession":{
"$ref":"3"
},
"Type":1,
"Value":45,
"ValueX":4
}
],
"Constants":[
{
"$id":15,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":17,
"Value":"8",
"Name":"8",
"AvailableOperant":231,
"Description":"8",
"StringReprsentation":"8"
},
{
"$id":16,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleListConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":263697,
"Name":"2",
"AvailableOperant":40,
"Value":[
1201,
1202
],
"Description":"2",
"StringReprsentation":"Male,Female"
},
{
"$id":17,
"Type":1049617,
"Value":1501,
"StringReprsentation":"Dealer",
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Description":"2",
"Name":"1"
},
{
"$ref":"8"
},
{
"$id":18,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleRangeConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":5393,
"Value":{
"$id":19,
"From":1,
"To":6
},
"Name":"12",
"AvailableOperant":16,
"Description":"12",
"StringReprsentation":"January AND June"
},
{
"$id":20,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":5137,
"Value":2,
"Name":"11",
"AvailableOperant":33,
"Description":"11",
"StringReprsentation":"February"
},
{
"$id":21,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleListConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":3601,
"Value":[
1,
2,
3,
0
],
"Name":"10",
"AvailableOperant":40,
"Description":"33",
"StringReprsentation":"Tuesday,Wednesday,Thursday,Monday"
},
{
"$id":22,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleRangeConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":3345,
"Value":{
"$id":23,
"From":0,
"To":4
},
"Name":"9",
"AvailableOperant":231,
"Description":"9",
"StringReprsentation":"Monday AND Friday"
},
{
"$id":24,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":3089,
"Value":0,
"Name":"7",
"AvailableOperant":225,
"Description":"7",
"StringReprsentation":"Monday"
},
{
"$ref":"5"
},
{
"$id":25,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.DateTime, mscorlib]], Payback.TradeTool.Entities",
"Type":33,
"Value":"2018-04-04T10:36:02.000Z",
"Name":"5",
"AvailableOperant":7,
"Description":"5",
"StringReprsentation":"4/4/2018"
},
{
"$id":26,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.Int32, mscorlib]], Payback.TradeTool.Entities",
"Type":17,
"Value":"4",
"Name":"4",
"AvailableOperant":231,
"Description":"4",
"StringReprsentation":"4"
},
{
"$id":27,
"$type":"Payback.TradeTool.Entities.Model.Offer.OfferRuleConstant`1[[System.String, mscorlib]], Payback.TradeTool.Entities",
"Type":9,
"Name":"3",
"AvailableOperant":289,
"Value":"3",
"Description":"3",
"StringReprsentation":"3"
}
],
"Variables":{
"$id":28
}
}
most of the Constants are parsing, but corresponding to "$id":17 is not in list.
after a few tries, i was able to find my mistake, It was "$id" property in json. which is number but in original JSON generated from API, it was string.
so, i changed my javaScript code generating JSON and it worked.
However I was unable to understand why it failed. when i posted with same data as generated form API initially, may be i just got confused.

Autofac Configuration for constructor with KeyFilter attribute

I am trying to use Autofac Configuration to create a Service object.
public class Service : IService
{
public Service([KeyFilter("eod")]ISimpleMongoClient eodClient,
[KeyFilter("live")]ISimpleMongoClient liveClient
) : base(config)
{
_eodClient = eodClient;
_liveClient = liveClient;
}
}
public class SimpleMongoClient : ISimpleMongoClient
{
public SimpleMongoClient(string connectionString, string database)
{
IMongoClient client = new MongoClient(connectionString);
MongoDatabase = client.GetDatabase(database);
}
}
Somehow with the following configuration, it is not able to resolve the ISimpleMongoClient parameters correctly. What else am I missing?
{
"components": [
{
"type": "Service, TestProject",
"services": [
{
"type": "IService, TestProject"
}
],
"instanceScope": "single-instance"
},
{
"type": "SimpleMongoClient, TestProject",
"services": [
{
"type": "ISimpleMongoClient, TestProject",
"key": "eod"
}
],
"parameters": {
"connectionString": "mongodb://localhost:27017/?readPreference=primary",
"database": "eod"
},
"instanceScope": "single-instance"
},
{
"type": "SimpleMongoClient, TestProject",
"services": [
{
"type": "ISimpleMongoClient, TestProject",
"key": "live"
}
],
"parameters": {
"connectionString": "mongodb://localhost:27017/?readPreference=primary",
"database": "live"
},
"instanceScope": "single-instance"
}
]
}
To use the KeyFilter attribute you need to register the thing doing the filtering with the WithAttributeFiltering() extension. You can't do that through configuration.

How to access Dynamodb's original JSON elements?

I am trying to test my lambda manually with the following dynamodb event input configured in tests -
Let's call this Json-1
{
"Records": [
{
"eventID": "1",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"NewImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES",
"SequenceNumber": "111",
"SizeBytes": 26
},
"awsRegion": "us-west-2",
"eventName": "INSERT",
"eventSourceARN": eventsourcearn,
"eventSource": "aws:dynamodb"
},
{
"eventID": "2",
"eventVersion": "1.0",
"dynamodb": {
"OldImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"SequenceNumber": "222",
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 59,
"NewImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"awsRegion": "us-west-2",
"eventName": "MODIFY",
"eventSourceARN": sourcearn,
"eventSource": "aws:dynamodb"
},
{
"eventID": "3",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 38,
"SequenceNumber": "333",
"OldImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"awsRegion": "us-west-2",
"eventName": "REMOVE",
"eventSourceARN": sourcearn,
"eventSource": "aws:dynamodb"
}
]
}
However, the json of dynamodb items look like this -
Let's call this Json-2
{
"id": {
"S": "RIGHT-aa465568-f4c8-4822-9c38-7563ae0cd37b-1131286033464633.jpg"
},
"lines": {
"L": [
{
"M": {
"points": {
"L": [
{
"L": [
{
"N": "0"
},
{
"N": "874.5625"
}
]
},
{
"L": [
{
"N": "1765.320601851852"
},
{
"N": "809.7800925925926"
}
]
},
{
"L": [
{
"N": "3264"
},
{
"N": "740.3703703703704"
}
]
}
]
},
"type": {
"S": "guard"
}
}
}
]
},
"modified": {
"N": "1483483932472"
},
"qastatus": {
"S": "reviewed"
}
}
Using the lambda function below, I can connect to my table. My goal is create a json which elastic search will accept.
#Override
public Object handleRequest(DynamodbEvent dynamodbEvent, Context context) {
List<DynamodbEvent.DynamodbStreamRecord> dynamodbStreamRecordlist = dynamodbEvent.getRecords();
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());
log.info("Whole event - "+dynamodbEvent.toString());
dynamodbStreamRecordlist.stream().forEach(dynamodbStreamRecord -> {
if(dynamodbStreamRecord.getEventSource().equalsIgnoreCase("aws:dynamodb")){
log.info("one record - "+dynamodbStreamRecord.getDynamodb().toString());
log.info(" getting N from new image "+dynamodbStreamRecord.getDynamodb().getNewImage().toString());
String tableName = getTableNameFromARN(dynamodbStreamRecord.getEventSourceARN());
log.info("Table name :"+tableName);
Map<String, AttributeValue> keys = dynamodbStreamRecord.getDynamodb().getKeys();
log.info(keys.toString());
AttributeValue attributeValue = keys.get("Id");
log.info("Value of N: "+attributeValue.getN());
Table table = dynamoDB.getTable(tableName);
}
});
return dynamodbEvent;
}
The format of a JSON item that elastic search expects is this and this is what I want to map the test input json to-
Let's call this Json-3
{
_index: "bar-guard",
_type: "bar-guard_type",
_id: "LEFT-b1939610-442f-4d8d-9991-3ca54685b206-1147042497459511.jpg",
_score: 1,
_source: {
#SequenceNumber: "4901800000000019495704485",
#timestamp: "2017-01-04T02:24:20.560358",
lines: [{
points: [[0,
1222.7129629629628],
[2242.8252314814818,
1254.702546296296],
[4000.0000000000005,
1276.028935185185]],
type: "barr"
}],
modified: 1483483934697,
qastatus: "reviewed",
id: "LEFT-b1939610-442f-4d8d-9991-3ca54685b206-1147042497459511.jpg"
}
},
So what I need is read Json-1 and map it to Json-3.
However, Json-1 does not seem to be complete i.e. it does not have information that a dynamodb json has - like points and lines in Json-2.
And so, I was trying to get a connection to the original table and then read this additional information of lines and points by using the ID.
I am not sure if this is the right approach. Basically, want to figure out a way to get the actual JSON that dynamodb has and not the one that has attribute types
How can I get lines and points from json-2 using java? I know we have DocumentClient in javascript but I am looking for something in java.
Also, came across a converter here but doesn't help me- https://github.com/aws/aws-sdk-js/blob/master/lib/dynamodb/converter.js
Is this something that I should use DynamoDBMapper or ScanJavaDocumentAPI for ?
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html#marshallIntoObjects-java.lang.Class-java.util.List-com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig-
If yes, I am a little lost how to do that in the code below -
ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
ScanResult result = dynamoDBClient.scan(scanRequest);
for(Map<String, AttributeValue> item : result.getItems()){
AttributeValue value = item.get("lines");
if(value != null){
List<AttributeValue> values = value.getL();
for(AttributeValue value2 : values){
//what next?
}
}
}
Ok, this seems to work for me.
ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
ScanResult result = dynamoDBClient.scan(scanRequest);
for(Map<String, AttributeValue> item : result.getItems()){
AttributeValue value = item.get("lines");
if(value != null){
List<AttributeValue> values = value.getL();
for(AttributeValue value2 : values){
if(value2.getM() != null)
{
Map<String, AttributeValue> map = value2.getM();
AttributeValue points = map.get("points");
List<AttributeValue> pointsvalues = points.getL();
if(!pointsvalues.isEmpty()){
for(AttributeValue valueOfPoint : pointsvalues){
List<AttributeValue> pointList = valueOfPoint.getL();
for(AttributeValue valueOfPoint2 : pointList){
}
}
}
}
}
}
}