0

Struggling to understand how to deserialize a json object sent from AWS DynamoDB. I'm able to successfully call dynamoDB.describeTable(describeTableInput!) and receive a detailed response ...

2017-02-08 20:30:36.054 AWS_Test[84241:3626357] AWSiOSSDK v2.5.0 [Debug] AWSURLResponseSerialization.m line:63 | -[AWSJSONResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response body:
{"Table":{"AttributeDefinitions":[{"AttributeName":"Author","AttributeType":"S"},{"AttributeName":"ISBN","AttributeType":"S"}],"CreationDateTime":1.486165623131E9,"GlobalSecondaryIndexes":[{"IndexArn":"arn:aws:dynamodb:ap-southeast-2:xxxxxxxxxxxxxxx:table/Books/index/ISBN-index","IndexName":"ISBN-index","IndexSizeBytes":94946,"IndexStatus":"ACTIVE","ItemCount":1780,"KeySchema":[{"AttributeName":"ISBN","KeyType":"HASH"}],"Projection":{"ProjectionType":"ALL"},"ProvisionedThroughput":{"NumberOfDecreasesToday":0,"ReadCapacityUnits":5,"WriteCapacityUnits":5}},{"IndexArn":"arn:aws:dynamodb:ap-southeast-2:xxxxxxxxxxxxxx:table/Books/index/Author-index","IndexName":"Author-index","IndexSizeBytes":94946,"IndexStatus":"ACTIVE","ItemCount":1780,"KeySchema":[{"AttributeName":"Author","KeyType":"HASH"}],"Projection":{"ProjectionType":"ALL"},"ProvisionedThroughput":{"NumberOfDecreasesToday":0,"ReadCapacityUnits":5,"WriteCapacityUnits":5}}],"ItemCount":1780,"KeySchema":[{"AttributeName":"ISBN","KeyType":"HASH"}],"ProvisionedThroughput":{"NumberOfDecreasesToday":0,"ReadCapacityUnits":5,"WriteCapacityUnits":5},"TableArn":"arn:aws:dynamodb:ap-southeast-2:xxxxxxxxxxxxxxxxx:table/Books","TableName":"Books","TableSizeBytes":94946,"TableStatus":"ACTIVE"}}

My function calling Dynamo ...

func describeTable() {

        let dynamoDB = AWSDynamoDB.default()
        let describeTableInput = AWSDynamoDBDescribeTableInput()
        describeTableInput?.tableName = "Books"

        let tableDescription = dynamoDB.describeTable(describeTableInput!) as! AWSTask<AnyObject>

        let jsonResult: NSDictionary = try JSONSerialization.JSONObjectWithData(Data, options: JSONSerialization.ReadingOptions.MutableContainers) as! NSDictionary

        print(jsonResult.object(forKey: "ItemCount")!)
        // let's dump everything to see what was returned
        dump(tableDescription)

    }

But I've been struggling for days now trying to work out how to deserialize the response and store in my own dictionary.

Anyone able to assist!? thx!

2 Answers 2

1

Have you tried using the SwiftyJSON library? Third party libraries will save you from a massive headache, especially when dealing with data. With it, your code will look a little like:

let json = JSON(jsonResult)
if let count = json["Table"]["GlobalSecondaryIndexes"]["ItemCount"].int {
  print(count) //your count should be accessible here
}

Remember that JSON values are nested, so simply calling json["ItemCount"] will not return what you are expecting.

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

Comments

0

After digging and screaming and digging and crying I found hints here : https://github.com/awslabs/aws-sdk-ios-samples/blob/master/DynamoDBObjectMapper-Sample/Swift/DynamoDBSampleSwift/DDBDynamoDBManager.swift

Refactoring my code got me to accessing the response values ..

func describeTable() {
        let dynamoDB = AWSDynamoDB.default()
        let describeTableInput = AWSDynamoDBDescribeTableInput()
        describeTableInput?.tableName = "Books"
        let describeTask = dynamoDB.describeTable(describeTableInput!)

        var localTask:AWSTask<AnyObject>?

        localTask = describeTask.continueOnSuccessWith(block: { task -> AnyObject? in

            let describeTableOutput:AWSDynamoDBDescribeTableOutput = task.result!
            let tableName = describeTableOutput.table!.tableName
            let tableStatus = describeTableOutput.table!.tableStatus
            let itemCount = describeTableOutput.table?.itemCount

            print("--------------------------")
            print(tableName! as String)
            if tableStatus == AWSDynamoDBTableStatus.active {
                print("Active")
            } else {
                print("Inactive")
            }
            print(itemCount as! Int)
            print("--------------------------")

            return localTask
        })
    }

Hope this helps someone else!

Comments

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.