So I have an Immutable js Record:
{
title: "item 1",
title2: "item 2",
title3: "item 3"
}
I want to update the keys, so it will become:
{
new title: "item 1",
title2: "item 2",
title3: "item 3"
}
You can use delete keyword, to delete a particular key and then use Object.assign to append it as below:
let hello1={ title: "item 1", title2: "item 2", title3: "item 3"};
delete hello1['title'];
console.log(Object.assign({'new title': "item 1"}, hello1));
let obj = Immutable.fromJS({
title: "item 1",
title2: "item 2",
title3: "item 3"
});
// One liner
obj = obj.set('new_title', obj.get('title')
.delete('title');
Related
I have an Ansible job that run on 2 or more urls. Each url returns the same variables with different values. Here is the JSON data registry of the job:
{
"msg": {
"results": [
{
"url": "http://0.0.0.1:xxx1",
"othervar": "othervar",
"othervar2": "othervar2",
"json": {
"messages": [
{
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"message": "This is message number 3",
"message2": "This is message2 number 3"
}
]
}
},
{
"url": "http://0.0.0.2:xxx2",
"othervar": "othervar",
"othervar2": "othervar2",
"json": {
"messages": [
{
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"message": "This is message number 3",
"message2": "This is message2 number 3"
}
]
}
}
]
}
}
I want to make a specific dict containing only the url variable and also message and message2 variables. I have 2 options for my expected results:
option 1:
"message": [
{
"url": "http://0.0.0.1:xxx1",
"content": [
{
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"message": "This is message number 3",
"message2": "This is message2 number 3"
}
]
},
{
"url": "http://0.0.0.2:xxx2",
"content": [
{
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"message": "This is message number 3",
"message2": "This is message2 number 3"
}
]
}
option 2:
"message": [
{
"url": "http://0.0.0.0:xxx1",
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"url": "http://0.0.0.0:xxx1",
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"url": "http://0.0.0.0:xxx1",
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"url": "http://0.0.0.0:xxx2",
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"url": "http://0.0.0.0:xxx2",
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"url": "http://0.0.0.0:xxx2",
"message": "This is message number 2",
"message2": "This is message2 number 2"
}
]
I am able to get each variable (url only, message only, message2 only) but how can i merge them into a dict like option 1 or option 2?
The easiest way is to start with option 1 which is the closest to your original data. You can for example declare it as var in your play:
my_results: "{{ results | json_query('[].{\"url\": url, \"content\": json.messages}') }}"
Note that json_query requires the community.general collection (usually available if you installed the ansible meta package) and pip install jmespath
This gives (debuging the above variable):
TASK [debug] *******************************************************************
ok: [localhost] => {
"my_results": [
{
"content": [
{
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"message": "This is message number 3",
"message2": "This is message2 number 3"
}
],
"url": "http://0.0.0.1:xxx1"
},
{
"content": [
{
"message": "This is message number 1",
"message2": "This is message2 number 1"
},
{
"message": "This is message number 2",
"message2": "This is message2 number 2"
},
{
"message": "This is message number 3",
"message2": "This is message2 number 3"
}
],
"url": "http://0.0.0.2:xxx2"
}
]
}
From there, If you need to loop on structure which looks like your option 2, it's very easy to acheive using the subelements lookup:
- name: loop on a structure looking like option 2
debug:
msg:
- "url is {{ item.0.url }}"
- "message one is {{ item.1.message }}"
- "message two is {{ item.1.message2 }}"
loop: "{{ q('subelements', my_results, 'content') }}"
loop_control:
label: "{{ item.0.url }}"
which gives:
TASK [loop on a structure looking like option 2] *******************************
ok: [localhost] => (item=item.0.url) => {
"msg": [
"url is http://0.0.0.1:xxx1",
"message one is This is message number 1",
"message two is This is message2 number 1"
]
}
ok: [localhost] => (item=item.0.url) => {
"msg": [
"url is http://0.0.0.1:xxx1",
"message one is This is message number 2",
"message two is This is message2 number 2"
]
}
ok: [localhost] => (item=item.0.url) => {
"msg": [
"url is http://0.0.0.1:xxx1",
"message one is This is message number 3",
"message two is This is message2 number 3"
]
}
ok: [localhost] => (item=item.0.url) => {
"msg": [
"url is http://0.0.0.2:xxx2",
"message one is This is message number 1",
"message two is This is message2 number 1"
]
}
ok: [localhost] => (item=item.0.url) => {
"msg": [
"url is http://0.0.0.2:xxx2",
"message one is This is message number 2",
"message two is This is message2 number 2"
]
}
ok: [localhost] => (item=item.0.url) => {
"msg": [
"url is http://0.0.0.2:xxx2",
"message one is This is message number 3",
"message two is This is message2 number 3"
]
}
Here is a playbook for a full test:
---
- hosts: localhost
gather_facts: false
vars:
# Your orig data minified
results: [{"url":"http://0.0.0.1:xxx1","othervar":"othervar","othervar2":"othervar2","json":{"messages":[{"message":"This is message number 1","message2":"This is message2 number 1"},{"message":"This is message number 2","message2":"This is message2 number 2"},{"message":"This is message number 3","message2":"This is message2 number 3"}]}},{"url":"http://0.0.0.2:xxx2","othervar":"othervar","othervar2":"othervar2","json":{"messages":[{"message":"This is message number 1","message2":"This is message2 number 1"},{"message":"This is message number 2","message2":"This is message2 number 2"},{"message":"This is message number 3","message2":"This is message2 number 3"}]}}]
my_results: "{{ results | json_query('[].{\"url\": url, \"content\": json.messages}') }}"
tasks:
- name: debug my_results var to check
debug:
var: my_results
- name: loop on a structure looking like option 2
debug:
msg:
- "url is {{ item.0.url }}"
- "message one is {{ item.1.message }}"
- "message two is {{ item.1.message2 }}"
loop: "{{ q('subelements', my_results, 'content') }}"
loop_control:
label: item.0.url
The following code loads your input json, then creates a new array with the structure you ask in option 1, by creating new items while iterating on your messages list:
---
- name: Playbook for infinispan Hosts
hosts: localhost
tasks:
- name: load json from file
shell: cat test.json
register: result
- name: save the input json data to a variable
set_fact:
jsondata: "{{ result.stdout | from_json }}"
- name: display the input dict
debug:
var: jsondata
- name: parse the json into new array
set_fact:
message: "{{ message|default([]) + [ { 'url': item.url, 'content': item.json.messages } ] }}"
loop: "{{ jsondata.msg.results }}"
- name: display the resulting data structure
debug:
var: message
The testrail-api have 4 method to update result.
add_result
add_result_for_case
add_results
add_results_for_cases
add_result >> How should I do let me can add result to step?
The TestRail documentation includes an example of how to add steps when using the add_result endpoint. You need to use the custom_step_results field as shown below:
{
"status_id": 5,
"comment": "This test failed",
"elapsed": "15s",
"defects": "TR-7",
"version": "1.0 RC1 build 3724",
"custom_step_results": [
{
"content": "Step 1",
"expected": "Expected Result 1",
"actual": "Actual Result 1",
"status_id": 1
},
{
"content": "Step 2",
"expected": "Expected Result 2",
"actual": "Actual Result 2",
"status_id": 2
}
]
}
I have a json that looks like this:
[
{
"auth": 1,
"status": "Active",
"userCustomAttributes": [
{
"customAttributeName": "Attribute 1",
"customAttributeValue": "Value 1"
},
{
"customAttributeName": "Attribute 2",
"customAttributeValue": "Value 2"
},
{
"customAttributeName": "Attribute 3",
"customAttributeValue": "Value 3"
}
],
},
{
"auth": 1,
"status": "Active",
"userCustomAttributes": [
{
"customAttributeName": "Attribute 1",
"customAttributeValue": "Value 1"
},
{
"customAttributeName": "Attribute 2",
"customAttributeValue": "Value 2"
},
{
"customAttributeName": "Attribute 3",
"customAttributeValue": "Value 3"
},
{
"customAttributeName": "Attribute 4",
"customAttributeValue": "Value 4"
}
],
}
]
I would like to parse this and have a css output that looks something like this:
authType, status, attribute 1, attribute 2, attribute 3, attribute 4
"1", "active", "value1", "value2", "value3",""
"1", "active", "value1", "value2", "value3","value 4"
The json has over 180k records in the array so it would need to loop through all of them. Some records don't have all the attributes. Some have all 4 yet some only have 1. I am hoping to show a null value in the csv for the records that don't have the attribute.
With your sample input, the following program, which does not depend on the ordering of the "attribute" keys:
jq -r '
["Attribute 1", "Attribute 2", "Attribute 3", "Attribute 4"] as $attributes
# Header row
| ["authType", "status"]
+ ($attributes | map( (.[:1] | ascii_upcase) + .[1:])),
# Data rows:
(.[]
| (INDEX(.userCustomAttributes[]; .customAttributeName)
| map_values(.customAttributeValue)) as $dict
| [.auth, .status] + [ $dict[ $attributes[] ] ]
)
| #csv
'
produces the following CSV:
"authType","status","Attribute 1","Attribute 2","Attribute 3","Attribute 4"
1,"Active","Value 1","Value 2","Value 3",
1,"Active","Value 1","Value 2","Value 3","Value 4"
You can easily modify this to emit a literal string of your choice in place of a JSON null value.
Explanation
$dict[ $a[] ] produces the stream of values:
$dict[ $a[0] ]
$dict[ $a[1] ]
...
This is used to ensure the columns are produced in the correct order, independently of the ordering or even presence of the keys.
I have a code :
var ListeSortie =
[
{
"Champ1": "Texte 1",
"Champ2": "Texte 2",
"Champ3": "Texte 3"
},
{
"Champ4": "Texte 4",
"Champ5": "Texte 5",
"Champ6": "Texte 6"
}
]
console.log('_______Liste 1_____________')
console.log(ListeSortie[0])
console.log('_______Liste 2_____________')
console.log(ListeSortie[1])
the Output :
_______Liste 1_____________
{Champ1: "Texte 1", Champ2: "Texte 2", Champ3: "Texte 3"}
_______Liste 2_____________
{Champ4: "Texte 4", Champ5: "Texte 5", Champ6: "Texte 6"}
My question : How can i do to have juste the value Name of my objet like this:
Champ1
Champ2
Champ3
Champ4
Champ5
Champ6
Many thanks in advance
You can loop through your array and use Object.keys method to get the keys.
https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
Here's the answer:
First we loop through the ListeSortie to get the list of object in the ListeSortie.
Then we use the method Object.keys to get the key from the object inside ListeSortie.
Lastly, we for each every keys array and console.log the value.
ListeSortie.forEach(x => Object.keys(x).forEach(y => console.log(y)))
I try to get a random object from a local Json file in my Ionic3 App.
Instead of displaying all "questions", "answers" and "hints" from my "cards", I would like to display only the values of a randomly selected card.
This is a dummy project and I am not sure what's the appropriate way to do this.
home.ts
cards: any;
getLocalData() {
this.http.get('assets/data/cards.json').map(res => res.json()).subscribe(res => {
this.cards = res.cards;
this.cards.rd = this.cards[Math.floor(Math.random() * this.cards.length)];
console.log(this.cards.rd);
},
(err) => {
alert("failed loading json data");
})
}
home.html
<button ion-button round (click)="getLocalData()">Charger</button>
<ion-list>
<ion-item *ngFor="let card of cards">
<h2>{{card.question}}</h2>
<h2>{{card.answer}}</h2>
<h2>{{card.hint}}</h2>
</ion-item>
</ion-list>
cards.json
{
"cards": [
{
"id": "1",
"question": "question 1",
"answer": "reponse 1",
"hint": "hint 1"
},
{
"id": "2",
"question": "question 2",
"answer": "reponse 2",
"hint": "hint 2"
},
{
"id": "3",
"question": "question 3",
"answer": "reponse 3",
"hint": "hint 3"
}
]
}