How can I put JSON in user_data in terraform? - json

If there is any way to put JSON like this
{"status":"0","serial":"0"}
to user_data without using file ?

you can use heredoc syntax
(below codes don't work in a real ec2 instance, just show you how to use heredoc in terrafrom configuration file. )
resource "aws_instance" "web" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
user_data = <<EOF
{"status":"0","serial":"0"}
EOF
}
tags {
Name = "HelloWorld"
}
}

If you are using JSON formatted configuration file, you can do like this
{
"variable": {
"user_data": {
"type": "map",
"default": {
"status":"0",
"serial":"0"
}
}
},
"resource": {
"aws_instance": {
"web": {
"user_data":"${var.user_data}"
}
}
}
}

Related

How to conditionally add key to json if it doesn't exist?

I have a json file with the following data:
{
"Index" : {
"example_user" : {
"important_key" : "leave_me_alone",
"blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
},
"another_user" : {
"blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
},
"extra_person" : {
"blah" : {
"more_stuff" : {
"ignore_this" : "and_this_too"
}
}
}
}
}
As you can see, important_key is missing from another_user and extra_person.
My goal is to conditionally add "imporant_key" : "" where it is missing but not replace any existing important_key values. The program i use is JQ (1.6) from https://stedolan.github.io/jq/.
After many failed attempts on jqplay.org and having intensively (re)searched the internet on how to get the job done , i've only managed to realize the following:
Filter:.Index[]+={"important_key":"data"}
Result
{
"Index": {
"example_user": {
"important_key": "data",
"blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
}
},
"another_user": {
"blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
},
"important_key": "data"
},
"extra_person": {
"blah": {
"more_stuff": {
"ignore_this": "and_this_too"
}
},
"important_key": "data"
}
}
}
I'm aware += (over)writes any existing key value (note example_user). I've been experimenting with multiple piped filters/operators, including |= and { }, but in the end, the above result was the closest i could get.
(unfortunately i can't seem to find the jqplay code snippet where i went "crazy" with everything)
The moment you have to conditionally do something, you have to do some sort of filtering, either using if or select or other means.
For every object you wish to inspect, check if the object has your important_key and add it when necessary. If it has the key, do nothing, otherwise set it.
.Index[] |= if has("important_key") then . else .important_key = $myImportantKey end

How to prevent escaping when adding JSON object with jq?

I am working with jq and I am trying to add a new JSON object to a new key to an existing file.
I have the following JSON file, foobarbaz.json :
{
"example":{
"name": "stackOverflowQuestion"
}
}
I want to add a new entry under example, so to get the following output in foobar.json
{
"example": {
"name": "stackOverflowQuestion",
"new": {
"newfield": {
"key": "value"
}
}
}
}
I am using the following commands in the terminal:
$ tempvar='{"newfield":{"key":"value"}}'
$ cat foobarbaz.json | jq '.example.new=env.tempvar' > foobar.json
However, the output in foobar.json is somewhat unexpected:
{
"example": {
"name": "stackOverflowQuestion",
"new": "{\"newfield\":{\"key\":\"value\"}}"
}
}
Why does jq wrap the curly brackets with quotes, and why does it escape the double quotes?
Use fromjson to convert your string (the format all environment variables are in!) to the corresponding data structure, by decoding it as JSON content.
tempvar='{"newfield":{"key":"value"}}' jq '.example.new=(env.tempvar | fromjson)' <<'EOF'
{
"example":{
"name": "stackOverflowQuestion"
}
}
EOF
...emits as output:
{
"example": {
"name": "stackOverflowQuestion",
"new": {
"newfield": {
"key": "value"
}
}
}
}
Use the --argjson option to pass the pre-existing JSON snippet as a variable to the filter.
$ jq --argjson x "$tempvar" '.example.new=$x' foobarbaz.json
{
"example": {
"name": "stackOverflowQuestion",
"new": {
"newfield": {
"key": "value"
}
}
}
}
Note that tempvar isn't strictly necessary and can be dropped, if you are only defining it for use with the filter:
$ jq '.example.new={newfield: {key: "value"}}' foobarbaz.json
{
"example": {
"name": "stackOverflowQuestion",
"new": {
"newfield": {
"key": "value"
}
}
}
}

convert a JSON to another w/ circe

I would like to convert this JSON
{
"l1k1": {
"l2k1": "l2v1",
"l2k2": 1
},
"l1k2": [
{
"e1l1": "e1v1",
"e1l2": "e1v2"
},
{
"e2l1": "e2v1",
"e2l2": "e2v2"
}
]
}
to this one
{
"papa": {
"l1k1c": {
"l2k1c": {
"string": "l2v1"
},
"l2k2c": {
"int": 1
}
},
"l1k2c": {
"array": [
{
"e1l1": "e1v1",
"e1l2": "e1v2"
},
{
"e2l1": "e2v1",
"e2l2": "e2v2"
}
]
}
}
}
where:
"l" stands for level
"k" for key, "v" for value
"e" for element
"c" for copy (where "*" maps to "*c")
I'm using circe's Json but having a hard time renaming the keys or creating parents or children with it. As I'm writing this, I'm thinking I may need to use its ACursor instead. As you may have guessed, I'm trying to generate an AVRO doc from an input JSON. I'm open to help w/ my approach or any suggestions about how to go about it in a cleaner way.

JSON liquid mapping using payload

I want to access the the below JSON payload :
[
{
"ApplicantContactDetail": [
{
"ApplicantBaseDetails": {
"CATEGORY": "P",
"ULN": "1265847478"
} }
]
}
]
and need output as
{
"Number":2,
"Data":
{
"ResidencyCode":"CATEGORY"
}
}
i need to map CATEGORY in the output from liquid template, may i know the correct syntax for this to achieve.

Forge ModelDerivativeAPI: can't convert to obj format

I'm trying to convert .nwc file uploaded on my A360 account with ModelDerivativeAPI with this method. While converting to svf and some other formats works for me perfectly I have troubles with converting to obj format. I keep getting the following 400 error:
{
"diagnostic": "Failed to trigger translation for this file." }
Here is the body of my POST request:
{
"input": {
"urn": "dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLjJFZWM1RzZsU1NXMXNnVmFUSW93Zmc_dmVyc2lvbj0x"
},
"output": {
"formats": [
{
"type": "obj"
}
]
}
}
What am I doing wrong?
Try to add the advanced field as follow:
{
"input": {
"urn": "dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLjJFZWM1RzZsU1NXMXNnVmFUSW93Zmc_dmVyc2lvbj0x"
},
"output": {
"formats": [
{
"type": "obj",
"advanced": {
"objectIds": [-1], // -1 means all nodes, otherwise specify an array of valid dbIds
"modelGuid": "ab2fb5b0-9101-0d3e-28c9-e61f9f0c15c5" // Get this one from the GET :urn/metadata call - https://developer.autodesk.com/en/docs/model-derivative/v2/reference/http/urn-metadata-GET/
}
}
]
}
}
You can use my sample https://oss.autodesk.io which exposes a UI to help you test and format the payloads:
Hope that helps