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

[Question] How to convert string to list or map #16868

Closed
LanceNero opened this issue Dec 7, 2017 · 9 comments
Closed

[Question] How to convert string to list or map #16868

LanceNero opened this issue Dec 7, 2017 · 9 comments

Comments

@LanceNero
Copy link

LanceNero commented Dec 7, 2017

Hi there,

Terraform Version

Terraform v0.10.7

Question

I have a json string, like this:

{
    "key1":"value1",
    "key2":["value2-1","value2-2"],
    "key3":[{ "subkey1":"subvalue1"},{"subkey2","subvalue2"},{"....":"...."}]
}

I can get the key/value pair , but how to convert json string to list or map ?
Currently, I can convert a string to a list, using the split method, but is there a better way?
And how to convert string to map?

Thanks for any help!

@LanceNero LanceNero changed the title [Question] How to use terraform to convert string to list or map [Question] How to convert string to list or map Dec 7, 2017
@LanceNero
Copy link
Author

Hi @apparentlymart
I have a string , like this :

    {"key1":"value1","key2":"value2"}

Do you have solution for this scenario?
Please help me, Thanks

@apparentlymart
Copy link
Contributor

Hi @LanceNero,

At the moment there is no built-in way to decode a JSON string. There is some discussion about this in #10363, including information about our plans here.

In the mean time, it is necessary to pass data into Terraform some other way, such as generating a .tfvars file containing your value to access it via an input variable.

@LanceNero
Copy link
Author

Hi @apparentlymart
Ok, got it
Thanks your reply

@oulydna
Copy link

oulydna commented Jan 10, 2019

in my code, I use External data source to convert a string to map. e.g: for string like {"key1":"value1","key2":"value2"}

data "external" "converer" {
  program = ["echo", "${var.str}"]
}

output "value1" {
  value = "${(lookup(data.external.converter.result, "key1")}"
}

The output would then be "value1".

@timduly4
Copy link

@oulydna is spot on, but I thought I would correct a few typos:

data "external" "converter" {
  program = ["echo", "${var.str}"]
}

output "value1" {
  value = "${lookup(data.external.converter.result, "key1")}"
}

If I saved someone 3 minutes, it's worth it 🎉

@kism3t
Copy link

kism3t commented Jun 25, 2019

@timduly4 And @oulydna I get the error in powershell

* data.external.converter: 1 error(s) occurred:
* data.external.converter: data.external.converter: can't find external program "echo"

Any Idea what I do wrong?

@timduly4
Copy link

@kism3t this indicates that you don't have echo on your machine-- are you on Windows? If so, you'll need to find the Windows equivalent...

@kism3t
Copy link

kism3t commented Jun 27, 2019

I run on windows within poweshell. There I have echo command.

@ghost
Copy link

ghost commented Jul 24, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants