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

Not able to get the string from list from external json data using jq (OCI) #21532

Closed
RanganathNangineni opened this issue May 31, 2019 · 3 comments
Labels

Comments

@RanganathNangineni
Copy link

RanganathNangineni commented May 31, 2019

Hi,
I have the below JSON data and I am not able to get the individual string from list of arrays.
Not sure whether my syntax is correct or not.

create_tags62716.json

{
    "tenancy_ocid": "xyz", 
    "compartment_name": "XYZ-CONTAINER", 
    "tag_namespace_id": "XYZNMMMLJLJJLJLJLJ", 
    "job_execid": "62960", 
    "tag_names": [
        "tagname1", 
        "tagname2", 
        "tagname3"
    ], 
    "compartment_ocid": "xyzabc, 
    "region": "iad", 
    "namespace": "default_NameSpace", 
    "tag_description": [
        "desc1", 
        "desc2", 
        "desc3"
    ], 
    "json_dir": "/home/iam-tags", 
    "tenancy_name": "sample1", 
    "job_name": "Tags :  create tags"
}

The below is the Terraform template also.

### external json data source
data "external" "create_tags62716" {
  program = ["jq", ".", "${path.module}/create_tags62716.json"]
  query   = {}
}

### create new tags
######SAMPLE #####3
resource "oci_identity_tag" "create_tags62716" {
    count = 3
    name = "${jsonencode(element(data.external.create_tags62716.result.tag_names, count.index))}"
    description = "${jsonencode(element(data.external.create_tags62716.result.tag_description, count.index))}"
    tag_namespace_id = "DEFAULT_NAMESPACE_ID"
    is_retired = false
 lifecycle {
    prevent_destroy = true
  }

}

Error

* module.iam-tags.data.external.create_tags62716: 1 error(s) occurred:
--
 
* module.iam-tags.data.external.create_tags62716: data.external.create_tags62716: command "jq" produced invalid JSON: json: cannot unmarshal array into Go value of type string
@apparentlymart
Copy link
Contributor

Hi @RanganathNangineni,

The external data source, as described in its documentation, only supports JSON string values. It does not support any other type, and it is not intended as a generic way to read JSON data but rather as a way to create Terraform-specific integrations with external software.

It seems like you are using this just to decode a specific file from disk, without doing any processing on it. If that is true, then I would recommend upgrading to Terraform 0.12 and then using the new jsondecode built-in function instead:

locals {
  create_tags = jsondecode(file("${path.module}/create_tags62716.json"))
}

resource "oci_identity_tag" "create_tags62716" {
  count = 3

  name             = local.create_tags.tag_names[count.index]
  description      = local.create_tags.tag_description[count.index]
  tag_namespace_id = "DEFAULT_NAMESPACE_ID"
  is_retired       = false

  lifecycle {
    prevent_destroy = true
  }
}

Please note that we use GitHub issues for tracking bugs and enhancements rather than for questions. While we may be able to help with certain simple problems here it's generally better to use one of the community forums where there are far more people ready to help, whereas the GitHub issues here are generally monitored only by our few core maintainers. If you have any follow-up questions, please use one of the community forums. Thanks!

@RanganathNangineni
Copy link
Author

Thanks for the alternative. I am able to achieve the required output with the below format.

external json data source

data "external" "create_tags64033" {
program = ["jq", ".", "${path.module}/create_tags64033.json"]
query = {}
}

create new tags

######SAMPLE #####3
resource "oci_identity_tag" "create_tags64033" {
count = "${length(split(",", data.external.create_tags64033.result["tag_names"]))}"
description = "${element(split(",", data.external.create_tags64033.result["tag_description"]),count.index)}"
name = "${element(split(",", data.external.create_tags64033.result["tag_names"]),count.index)}"
tag_namespace_id = "${data.external.create_tags64033.result["tag_namespace_id"]}"
is_retired = false
lifecycle {
prevent_destroy = true
}

}

@ghost
Copy link

ghost commented Jul 25, 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 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants