Reedza
Reedza

Reputation: 11

How to extract data from this type of character in R?

"{\\\"order\\\":[{\\\"product_id\\\":29,\\\"quantity\\\":1,\\\"product_name\\\":\\\"Almond Butter\\\",\\\"unit\\\":\\\"Unit\\\",\\\"price\\\":16,\\\"mode_delivery\\\":\\\"2\\\",\\\"merchant_id\\\":26,\\\"productImg\\\":\\\"https://prod-emeniaga.s3.ap-southeast-1.amazonaws.com/product_photo/26-1598338281196.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAYIKU5V3ERKVOQKOL%2F20200825%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Date=20200825T065527Z&X-Amz-SignedHeaders=host&X-Amz-Expires=300&X-Amz-Signature=de53812cec08b2b44f28ee0e7a20be0f959ab2c39cecc080c2e328c8fd5a1e2a\\\",\\\"photo\\\":\\\"26-1598338281196.png\\\"}]}"

I want to get the product_name from the above. Anyone know how to do it in R?

Upvotes: 0

Views: 41

Answers (1)

clemens
clemens

Reputation: 6813

You can use jsonlite::fromJSON to parse the string and then subset the resulting object to get the product name:

library(jsonlite)

txt <- "{\"order\":[{\"product_id\":29,\"quantity\":1,\"product_name\":\"Almond Butter\",\"unit\":\"Unit\",\"price\":16,\"mode_delivery\":\"2\",\"merchant_id\":26,\"productImg\":\"https://prod-emeniaga.s3.ap-southeast-1.amazonaws.com/product_photo/26-1598338281196.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAYIKU5V3ERKVOQKOL%2F20200825%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Date=20200825T065527Z&X-Amz-SignedHeaders=host&X-Amz-Expires=300&X-Amz-Signature=de53812cec08b2b44f28ee0e7a20be0f959ab2c39cecc080c2e328c8fd5a1e2a\",\"photo\":\"26-1598338281196.png\"}]}"

parsed <- fromJSON(txt=txt)

parsed$order$product_name

And the result is: [1] "Almond Butter"

Upvotes: 2

Related Questions