I'm using jq 1.6 on Windows 7 and want to add a dynamically generated array to a json file.
That array doesn't yet exist in this file.
I've got the following JSON structure (reduced for reasons of clarity):
{
"policies": {
"SearchBar": "separate",
"SearchEngines": {
"PreventInstalls": false
}
}
}
I'd like to generate an array based on dynamic values and finally create the following output:
{
"policies": {
"SearchBar": "separate",
"SearchEngines": {
"PreventInstalls": false,
"Remove": [
"Twitter",
"Wikipedia (en)"
]
}
}
}
The Remove array's content is stored in a (cmd) %variable%.
I found that the line
jq -n --arg items "%variable%" "{ Remove: $items | split(\",\") }"
produces the array I want:
{
"Remove": [
"Twitter",
"Wikipedia (en)"
]
}
What is the best way to insert this array into the original file?
Given the string input string Twitter,Wikipedia (en), you can use jq to update the JSON data:
<file jq --arg i 'Twitter,Wikipedia (en)' '.policies.SearchEngines += ({ Remove: $i | split(",") })'
{
"policies": {
"SearchBar": "separate",
"SearchEngines": {
"PreventInstalls": false,
"Remove": [
"Twitter",
"Wikipedia (en)"
]
}
}
}
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"
}
}
}
}
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.
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}"
}
}
}
}
I want to create a json string for my web application. Actually i am new to this json format.In my json string i have to create two arrays in my json structure.But i have some syntax problem in creating two arrays. my json string is given below for your reference.
{
MarkUpdate:[
{
'FinalMarks':[
{
'studentId':'S1',
'Ques_Mark':[
{
'qId' :'Q1',
'mark':'14',
},
{
'qId':'Q2',
'mark':'10',
}
]
},
{
'studentId':'S2',
'Ques_Mark':[
{
'qId' :'Q1',
'mark':'12',
},
{
'qId':'Q2',
'mark':'13',
}
]
}
]
}
]
}
In my above json string format,my MarkUpdate contains one array object named as FinalMarks.So,here i have to create one more array object named as EvalMarks under MarkUpdate.
Acually my EvalMarks contains following elements...
'EvalMarks':[
{
'EvalId':'E1',
'Ques_Mark':[
{
'qId' :'Q1',
'studId':'S1',
'mark':'13',
},
{
'qId':'Q2',
'studId':'S1',
'mark':'13',
}
]
},
{
'EvalId':'E2',
'Ques_Mark':[
{
'qId' :'Q1',
'studId':'S2',
'mark':'10',
},
{
'qId':'Q2',
'studId':'S2',
'mark':'10',
}
]
}
]
So, i have declare this EvalMarks under the MarkUpdate.I missed the syntax...
Could you plz tell me how to add this array object under the MarkUpdate.
guide me to get out of this issue...
To declare two arrays in one JSON object, remember that the JSON object can only be a single object, therefore the array must be inside the enclosing curly braces. For example:
{
"array1":[1,2,3],
"array2":["jim","louise","mark"]
}
For your case, it's important to remember that you should properly indent your braces, square and curly, so that you can visually identify mistakes before they become problems. I stringly recommend http://jslint.com/ to validate your JSON before using it. It's also great for Javascript:
{
"MarkUpdate":[
{
"FinalMarks":[
{
"studentId":"S1",
"Ques_Mark":[
{
"qId" :"Q1",
"mark":"14"
},
{
"qId":"Q2",
"mark":"10"
}
]
},
{
"studentId":"S2",
"Ques_Mark":[
{
"qId" :"Q1",
"mark":"12"
},
{
"qId":"Q2",
"mark":"13"
}
]
}
]
}
],
"EvalMarks":[
{
"EvalId":"E1",
"Ques_Mark":[
{
"qId" :"Q1",
"studId":"S1",
"mark":"13"
},
{
"qId":"Q2",
"studId":"S1",
"mark":"13"
}
]
},
{
"EvalId":"E2",
"Ques_Mark":[
{
"qId" :"Q1",
"studId":"S2",
"mark":"10"
},
{
"qId":"Q2",
"studId":"S2",
"mark":"10"
}
]
}
]
}
Its not a valid JSON if you have commas after the last key-value pair in an object. I would start by knocking off all those unnecessary commas after the last key-value pairs inside most of your objects and validating the JSON in www.jslint.com
To be more clear ,
{
"qId":"Q2",
"studId":"S2",
"mark":"10",
}
is Not Valid.
On the other hand,
{
"qId":"Q2",
"studId":"S2",
"mark":"10"
}
Is valid.