Search code examples
c#jsonamazon-dynamodbasp.net-web-api2

Convert dynamodb json to normal json using C#


Am trying to convert dynamodb json to normal json using C#, actually for the object type M am unable to achieve the conversion still getting the result as below

{
    "RecordDetails": {
        "firstName": {
            "Value": “32432-ere”,
            "Type": 0
        },
        "lastName": {
            "Value": "32432-ere",
            "Type": 0
        },
        "contactInformation": {
            "addressLine1": {
                "Value": "10 Downing st",
                "Type": 0
            },
            "addressLine2": {
                "Value": "",
                "Type": 0
            },
            "city": {
                "Value": "Portand",
                "Type": 0
            },
            "phone": {
                "Value": "555-555-5555",
                "Type": 0
            },
            "email": {
                "Value": "[email protected]”,
                "Type": 0
            }
        },
        "employer": {
            “Id”: {
                "Value": “test:1473969",
                "Type": 0
            }
        },
        "middleName": {
            "Value": "A",
            "Type": 0
        },
        "id": {
            "Value": "637934995361318864",
            "Type": 0
        },
        "title": {
            "Value": "Dr.",
            "Type": 0
        },
        "userId": {
            "Value": “ererew-2332432”,
            "Type": 0
        },
        "earnedDegrees": {
            "Value": "MD.",
            "Type": 0
        }
    },
    "ValidationStatus": {
        "Value": "Passed",
        "Type": 0
    },
    "TypeOfRecord": {
        "Value": “test”,
        "Type": 0
    },
    "TenantCode": {
        "Value": “demo2”,
        "Type": 0
    },
    "Action": {
        "Value": "create",
        "Type": 0
    },
    "TenantHostName": {
        "Value": “t1.demo.com",
        "Type": 0
    },
    "ImportJobId": {
        "Value": “erewre7777”-322h23h,
        "Type": 0
    },
    "ValidationMessage": {
        "Value": "No Errors",
        "Type": 0
    },
    "ProcessingResult": {
        "Value": "Fail",
        "Type": 0
    },
    "ProcessedAt": {
        "Value": "2022-07-20T10:07:43.650Z",
        "Type": 0
    },
    "RecordId": {
        "Value": “dfdsf-3242-sdfds”,
        "Type": 0
    },
    "ApiResponse": {
        "errors": {
            "Entries": [
                {
                    "code": {
                        "Value": "-1",
                        "Type": 1
                    },
                    "internalErrorMessage": {
                        "Value": "",
                        "Type": 0
                    },
                    "links": {
                        "about": {
                            "Value": "",
                            "Type": 0
                        },
                        "help": {
                            "Value": "",
                            "Type": 0
                        }
                    },
                    "detail": {
                        "Entries": [
                            {
                                "Value": "23505: duplicate key value violates ",
                                "Type": 0
                            }
                        ]
                    },
                    "source": {
                        "Value": “test Service",
                        "Type": 0
                    },
                    "incidentId": {},
                    "status": {
                        "Value": "500",
                        "Type": 1
                    }
                }
            ]
        }
    } }

Actually was using the Document.FromAttributeMap() to get the above result, but I still need to make it look like a normal json value like

 {
    "RecordDetails": {
        "firstName": “32432-ere”,
        "lastName": "32432-ere",
        "contactInformation": {
            "addressLine1": "10 Downing st",
            "addressLine2": “”,
            "city": "Portand",
            "phone": "555-555-5555",
            "email": "[email protected]”
        },
        "employer": {
            “Id”: “test:1473969"
        },
        "middleName": "A",
        "id": "637934995361318864",
        "title": "Dr.",
        "userId": “ererew-2332432”,
        "earnedDegrees": "MD.",        },
    "ValidationStatus":"Passed",
    "TypeOfRecord": “test”,
    "TenantCode": “demo2”,
    "Action":  "create",
    "TenantHostName": “t1.demo.com",
    "ImportJobId": “erewre7777-322h23h”,
    "ValidationMessage": "No Errors",
    "ProcessingResult": "Fail",
    "ProcessedAt": "2022-07-20T10:07:43.650Z",
    "RecordId":“dfdsf-3242-sdfds”,
    "ApiResponse": {
        "errors": {
            "Entries": [
                {
                    "code": "-1",
                    "internalErrorMessage": “”,
                    "links": {
                        "about": “”,
                        "help”:””                        },
                    "detail": {
                        "Entries": [
                            {
            "23505: duplicate key value violates "
                            }
                        ]
                    },
                    "source": “test Service",
                    "incidentId": {},
                    "status": “500”
                }
            ]
        }
    } }

Can anyone help me to achieve this like by suggesting some nuget packages or by some manual process, tried a lot still unable to achieve this, sorry if this is a duplicated questions.

Thanks in advance


Solution

  • You should get pretty JSON from the document object using ToJsonPretty extension method.

    Document document = Document.FromAttributeMap(...); 
    string jsonString = document.ToJsonPretty(); <--- This is what you need
    

    You would need Amazon.DynamoDBv2.DocumentModel namespace for this message.