Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query() function from dynamodb() #402

Closed
jtruffa opened this issue Apr 9, 2021 · 3 comments
Closed

query() function from dynamodb() #402

jtruffa opened this issue Apr 9, 2021 · 3 comments

Comments

@jtruffa
Copy link

jtruffa commented Apr 9, 2021

Hi,
query() returns me a nested list.
in the Items sections, every attribute of a record is itself a list.

` $ :List of 9
  ..$ version     :List of 10
  .. ..$ S   : chr "1"
  .. ..$ N   : chr(0) 
  .. ..$ B   : chr(0) 
  .. ..$ SS  : list()
  .. ..$ NS  : list()
  .. ..$ BS  : list()
  .. ..$ M   : list()
  .. ..$ L   : list()
  .. ..$ NULL: logi(0) 
  .. ..$ BOOL: logi(0) `

Is there any way of telling query() to return just the attribute with its corresponding value?

I couln't find it in the doc.

thanks,

@davidkretch
Copy link
Member

davidkretch commented Apr 11, 2021

Sorry about that. Unfortunately there isn't anything built in -- Paws doesn't do anything extra with what DynamoDB returns, at least at the moment.

However, I put together the flatten function below which may help. It will flatten your list to show only the attribute names and values.

# Example DynamoDB result data.
a <- list(
  Foo = list(
    S = character(0),
    N = list(),
    B = list(),
    L = list(),
    M = list(
      Bar = list(
        S = "1",
        N = list(),
        B = list(),
        L = list(),
        M = list()
      )
    )
  )
)

# Flatten a DynamoDB query response, into an object that has only attribute names and values. 
flatten <- function(x) {
  result <- list()
  for (name in names(x)) {
    result[[name]] <- get_value(x[[name]])
  }
  return(result)
}

# Get the value from a single attribute.
get_value <- function(x) {
  element <- names(x)[sapply(x, function(el) length(el) > 0)]
  if (element == "M") return(flatten(x[[element]]))
  return(x[[element]])
}

# Test the flatten function on the example data.
b <- flatten(a)

# > str(b)
# List of 1
#  $ Foo:List of 1
#   ..$ Bar: chr "1"

@jmtruffa
Copy link

Hi David,
Thanks for the solution you have provided!
With a little tweak, since the response that I got has one more level, I finally obtained a more usable list (put it in a for loop)

Don't know why dynamo returns the full list of data types like this.

You can close the issue.
Great work!

@davidkretch
Copy link
Member

Awesome! Glad to hear it.

My guess about that response shape is I think it's designed for languages that have fixed data types, so they have a slot for strings, for integers, etc.

Thanks for bringing it up. This is a good example and if I get some time, I'll put it in the examples folder too.

Have a good one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants