2

I have a Swift function that performs a batchGetItem in AWS DynamoDB. The output is being returned in a JSON format, however I am having difficulty parsing it. Below is the code that I am trying to use to parse the data, as well as what the JSON output looks like. Any help is much appreciated. Thanks.

Function:

func batchGetItem () {

        let DynamoDB = AWSDynamoDB.default()

        var keys = [Any]()

        let idNumbers = Array
        let n = 0

        for i in 0...n {

            let value = AWSDynamoDBAttributeValue()
            value?.n = String(idNumbers[i])
            keys.append(["numID": value])

        }
let keysAndAttributesMap = AWSDynamoDBKeysAndAttributes()
        keysAndAttributesMap?.keys = keys as? [[String : AWSDynamoDBAttributeValue]]
        keysAndAttributesMap?.consistentRead = true
        let tableMap = ["tableName" : keysAndAttributesMap]
        let request = AWSDynamoDBBatchGetItemInput()
        request?.requestItems = tableMap as? [String : AWSDynamoDBKeysAndAttributes]
        request?.returnConsumedCapacity = AWSDynamoDBReturnConsumedCapacity.total
        DynamoDB.batchGetItem(request!) { (output, error) in

            if output != nil {
                print("Batch Query output?.responses?.count:", output!.responses!)
                print(output!.responses!)

                do {
                    let json = try JSONSerialization.jsonObject(with: output, options: .allowFragments) as! [String:Any]
                    let posts = json["posts"] as? [[String: Any]] ?? []
                    print(posts)
                } catch let error as NSError {
                    print(error)
                }
            }
            if error != nil {
                print("Batch Query error:", error!)
            }
        }
    }

Xcode Error Message Ambiguous reference to member 'jsonObject(with:options:)'

JSON Output

["tableName": 
    [
        [
            "hashKey":  < AWSDynamoDBAttributeValue: 0x1c00b3e60 > {
                N = 1;
            }, 
            "foo":  < AWSDynamoDBAttributeValue: 0x1c40b4b20 > {
                S = dog;
            }, 
            "numID":  < AWSDynamoDBAttributeValue: 0x1c00b3e00 > {
                N = 1;
            }, 
            "name":  < AWSDynamoDBAttributeValue: 0x1c80b55a0 > {
                S = "John Doe";
            }, 
            "link":  < AWSDynamoDBAttributeValue: 0x1c40b4b80 > {
                S = "http://";
            }, 
            "idNum":  < AWSDynamoDBAttributeValue: 0x1c80b5660 > {
                N = 67343;
            }
        ]
    ]
]

1 Answer 1

-2

You can use the DynamoDB Object Mapper to parse the data for you. https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-nosql-database.html#add-aws-mobile-nosql-database-crud

A sample setup for the data class would be like this:

import Foundation
import UIKit
import AWSDynamoDB

class Post: AWSDynamoDBObjectModel, AWSDynamoDBModeling {

    @objc var foo: String?
    @objc var numID: String?
    @objc var name: String?
    @objc var link: String?
    @objc var idNum: String?

    class func dynamoDBTableName() -> String {

        return "MY_TABLE"
    }

    class func hashKeyAttribute() -> String {

        return "numId"
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

You give an example of an object mapper but not how to use it to parse the data. -1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.