The method toJSONPretty() is undefined for the type Map<String,AttributeValue>

0

Hello I've a jsp with code (from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.html) :

...

<%@ page import="com.amazonaws.services.dynamodbv2.AmazonDynamoDB" %>
<%@ page import="com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder" %>
<%@ page import="com.amazonaws.services.dynamodbv2.document.DynamoDB" %>
<%@ page import="com.amazonaws.services.dynamodbv2.document.Item" %>
<%@ page import="com.amazonaws.services.dynamodbv2.document.ItemCollection" %>
<%@ page import="com.amazonaws.services.dynamodbv2.document.ScanOutcome" %>
<%@ page import="com.amazonaws.services.dynamodbv2.document.Table" %>
<%@ page import="com.amazonaws.services.dynamodbv2.model.AttributeValue" %>
<%@ page import="com.amazonaws.services.dynamodbv2.model.ScanRequest" %>
<%@ page import="com.amazonaws.services.dynamodbv2.model.ScanResult" %>


...

<%

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
        DynamoDB dynamoDB = new DynamoDB(client);
        String tableName = "TicketsWalletUsersDev";

        // Table table = dynamoDB.getTable(tableName);

        
        Map<String, AttributeValue> lastKeyEvaluated = null;
        do {
            ScanRequest scanRequest = new ScanRequest()
                .withTableName(tableName)
                .withLimit(10)
                .withExclusiveStartKey(lastKeyEvaluated);

            ScanResult result = client.scan(scanRequest);
            for (Map<String, AttributeValue> item : result.getItems()){                              
                System.out.println(item.toJSONPretty());
            }
            lastKeyEvaluated = result.getLastEvaluatedKey();
        } while (lastKeyEvaluated != null);

%>

It gives the error:

The method toJSONPretty() is undefined for the type Map<String,AttributeValue>

I want the string containing the true json and not the string returned from item.toString() that is :

{addresses={M: {mobile+75@nearit.com={M: {requestId={S: 909b1963-0ed4-4ac1-9b71-846239ed5bf6_1583142338893,}, verified={BOOL: true}},}, federico+11@nearit.com={M: {requestId={S: 909b1963-0ed4-4ac1-9b71-846239ed5bf6_1581332986414,}, verified={BOOL: true}},}, federico+signup3@nearit.com={M: {requestId={S: 909b1963-0ed4-4ac1-9b71-846239ed5bf6_1581330211793,}, verified={BOOL: true}},}, federico+33@nearit.com={M: {requestId={S: 909b1963-0ed4-4ac1-9b71-846239ed5bf6_1581330013122,}, verified={BOOL: true}},}, mobile@nearit.com={M: {requestId={S: 909b1963-0ed4-4ac1-9b71-846239ed5bf6_1583162677615,}, verified={BOOL: true}},}, federico+12@nearit.com={M: {requestId={S: 909b1963-0ed4-4ac1-9b71-846239ed5bf6_1581333042026,}, verified={BOOL: true}},}, federico+2@nearit.com={M: {requestId={S: 909b1963-0ed4-4ac1-9b71-846239ed5bf6_1581332941950,}, verified={BOOL: true}},}},}, created={S: 2020-02-05T10:40:17.887Z,}, userName={S: 909b1963-0ed4-4ac1-9b71-846239ed5bf6,}}

Thanks!

asked 2 years ago501 views
1 Answer
0
Accepted Answer

There is no toJsonPretty() for Map<String, AttributeValue> in your code.

To get json, you should get Item first from the table. You can refer the code example below Specifying Optional Parameters part here https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIGetItem

SUPPORT ENGINEER
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions