React Hooks update nested JSON array by index - arrays

I have this JSON object and I want to change an attribute inside it. In this case I'm trying to change VALUE inside configs[0].configs[0].value
{
"id": "PAY_TOOL_1",
"name": "PAY_TOOL_1",
"description": "PayTool1",
"state": "ENABLED",
"configs": [
{
"isDefaultConfig": null,
"id": "configId-1",
"name": "configId-1",
"defaultConfig": null,
"description": null,
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl",
"value": "http://localhost:7070/pay/payment/confirm/", <-- WANT TO CHANGE THIS
"defaultValue": null,
"description": null,
"editable": true
},
{
"isEditable": true,
"identifier": null,
"name": "cancelUrl",
"value": "http://localhost:7070/pay/payment/cancel/",
"defaultValue": null,
"description": null,
"editable": true
}
]
},
{
"isDefaultConfig": null,
"id": "configId-2",
"name": "configId-2",
"defaultConfig": null,
"description": null,
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl2",
"value": "http://localhost:7070/pay/payment/confirm/22",
"defaultValue": null,
"description": null,
"editable": true
},
{
"isEditable": true,
"identifier": null,
"name": "cancelUrl2",
"value": "http://localhost:7070/pay/payment/cancel/22",
"defaultValue": null,
"description": null,
"editable": true
}
]
}
]
}
This is the code in REACT HOOKS following this solution
SOLUTION
const [editPaymentTool, setEditPaymentTool] = useState(null);
function handleConfigurationInputs( configIndex, propertyIndex, configId, attributeName, attributeValue) {
console.log("CONFIG_INDEX: "+configIndex+" PROPERTY_INDEX: "+propertyIndex+" CONFIG ID: "+configId+ " NAME: "+attributeName+ " VALUE: "+attributeValue);
console.log(editPaymentTool);
setEditPaymentTool(prev => ({
...prev,
configs:[{
...prev.configs,
configs:[{
...prev.configs[configIndex].configs[propertyIndex],
value:attributeValue
}]
}]
}));
}
and the output produced which is pretty different from the expected above
{
"id": "PAY_TOOL_1",
"name": "PAY_TOOL_1",
"description": "PayTool1",
"state": "ENABLED",
"configs": [
{
"0": {
"isDefaultConfig": null,
"id": "configId-1",
"name": "configId-1",
"defaultConfig": null,
"description": null,
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl",
"value": "http://localhost:7070/pay/payment/confirm/", <-- EXPECTED TO BE CHANGED BUT NOT
"defaultValue": null,
"description": null,
"editable": true
},
{
"isEditable": true,
"identifier": null,
"name": "cancelUrl",
"value": "http://localhost:7070/pay/payment/cancel/",
"defaultValue": null,
"description": null,
"editable": true
}
]
},
"1": {
"isDefaultConfig": null,
"id": "configId-2",
"name": "configId-2",
"defaultConfig": null,
"description": null,
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl2",
"value": "http://localhost:7070/pay/payment/confirm/22",
"defaultValue": null,
"description": null,
"editable": true
},
{
"isEditable": true,
"identifier": null,
"name": "cancelUrl2",
"value": "http://localhost:7070/pay/payment/cancel/22",
"defaultValue": null,
"description": null,
"editable": true
}
]
},
"configs": [
{
"isEditable": true,
"identifier": null,
"name": "returnUrl",
"value": "http://localhost:7070/pay/payment/confirm/l", <-- THE CHANGED VALUE IS PUT HERE (WRONG)
"defaultValue": null,
"description": null,
"editable": true
}
]
}
]
}
Any help is appreciated

Try this:
newState = _.cloneDeep(editPaymentTool);
newState.configs[0].configs[0].value = newValue;
//more complicated nested updates
...
setEditPaymentTool(newState);

Related

Adding elements within Object TypeScript

Hello everyone i have dynamically expanding object which consists of a 2 x dimensional array. I
try to add in field label:{ } new key:value {"hg":"Hg"} object.
object JSON:
[
{
"callbackQueryData": "tNLQy3VcX",
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "Cll8xZbVo6",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
},
"label": {
"en": "New button"
}
},
{
"callbackQueryData": "qntufUVhz",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[
{
"callbackQueryData": "cx46ECYG9",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[
{
"callbackQueryData": "uHp5yd3Li",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[]
]
I try cast it to simple array and via forEach() addressing everyone elements button:any and to add object which i need. But Spread syntax(...) can't find argument forEach().Or I'm doing it completely wrong.
let arrayButton:IBotButton[][] = ([] as IBotButton[][]).concat(...page.callbacks)//create simple array
arrayButton.forEach((button:any)=>{
...button,
label: { ...button.label, [languageSelect]: buttonText }
})
Assume your arrayButton looks like this:
const arrayButton: IBotButton[][] = [
[
{
label: { "en": "new Button-1" }
// ...
},
{
label: { "en": "new Button-2" }
// ...
},
//...
],
[
//...
]
];
And have these variables:
const languageSelect: string = "jp";
const buttonText: string = "new Button-3";
Then you could modify the label by following code:
// arrayButton is 2d array
arrayButton.forEach((array) => {
// array is 1d array
array.forEach((button, buttonIdx) => {
array[buttonIdx] = {
...button,
label: {
...button.label,
[languageSelect]: buttonText,
},
};
});
});

Reading JSON object data and how to filter?

Currently I'm developing an application which uses Prepr.io as API for fetching my own videos which are also stored in Prepr.io. When I'm fetching data with this link: https://cdn.prepr.io/publications?fields=items{file{cdn_files}} I get the following JSON as response:
Object {
"after": "YWZ0ZXJfMjU=",
"before": "YmVmb3JlXzA=",
"items": Array [
Object {
"changed_on": "2022-02-28T12:16:31+00:00",
"created_on": "2022-02-28T12:16:31+00:00",
"id": "905f950f-3e18-4b40-8c72-f8d3e509e546",
"items": Object {
"en-US": Object {
"beschikbaar_op_route": Object {
"items": Array [
Object {
"body": "Route Groningen stadspark",
"changed_on": null,
"created_on": "2022-02-28T12:13:10+00:00",
"id": "a3758c72-1074-4385-884e-34c4bd7d6e1d",
"label": "Tag",
"slug": "route-groningen-stadspark",
},
Object {
"body": "Route Leeuwarden Kalverdijkje",
"changed_on": null,
"created_on": "2022-02-28T12:13:28+00:00",
"id": "cc3cbe4b-536a-40e7-b69b-8bdddc24d445",
"label": "Tag",
"slug": "route-leeuwarden-kalverdijkje",
},
],
"label": "Tag",
},
"file": Object {
"items": Array [
Object {
"author": null,
"body": null,
"cdn_files": Object {
"items": Array [
Object {
"bucket": null,
"cdn": "BunnyCdn",
"changed_on": null,
"created_on": "2021-05-11T13:02:22+00:00",
"extension": null,
"file": "c7f6339d-e686-41a1-82ef-e3fbb9ebb425.mp4",
"id": "51fbad9a-70a9-4f6e-abfb-59b7a3ab2857",
"label": "CdnFile",
"presets": Array [],
"profile": null,
"url": "https://5ge7hcv8ge1k.b-cdn.net/c7f6339d-e686-41a1-82ef-e3fbb9ebb425.mp4",
},
],
"total": 1,
},
"changed_on": "2022-01-06T15:03:34+00:00",
"created_on": "2021-05-11T13:02:07+00:00",
"duration": 32640,
"ended_on": null,
"extension": "mp4",
"height": 720,
"id": "c7f6339d-e686-41a1-82ef-e3fbb9ebb425",
"label": "Video",
"mime_type": "video/mp4",
"name": "Stang in de nek video",
"original_name": "Stang in de nek",
"reference_id": null,
"replaceable": true,
"started_on": null,
"status": null,
"width": 1280,
},
],
"label": "Asset",
},
"image": Object {
"items": Array [
Object {
"author": null,
"body": null,
"changed_on": "2021-03-16T08:41:28+00:00",
"created_on": "2021-03-16T08:41:28+00:00",
"extension": "jpg",
"height": 1280,
"id": "9cade08f-5d16-41d1-8007-d18a08f0cddd",
"label": "Photo",
"mime_type": "image/jpeg",
"name": "b053b802-abf8-47b4-888a-9304712c0a41",
"original_name": "b053b802-abf8-47b4-888a-9304712c0a41",
"reference_id": null,
"replaceable": true,
"status": null,
"width": 1920,
},
],
"label": "Asset",
},
"kenmerken": Object {
"items": Array [
Object {
"body": "Rollator",
"changed_on": null,
"created_on": "2022-01-06T14:48:37+00:00",
"id": "0af8af83-d353-4d05-aff2-b86be1bf12b8",
"label": "Tag",
"slug": "rollator",
},
],
"label": "Tag",
},
"niveau": Object {
"items": Array [
Object {
"body": "Level 1",
"changed_on": null,
"created_on": "2021-05-06T10:12:28+00:00",
"id": "5d7cb8be-7bb2-4b96-9910-744303470e81",
"label": "Tag",
"slug": "level-1",
},
Object {
"body": "Level 2",
"changed_on": null,
"created_on": "2021-05-06T10:12:30+00:00",
"id": "eb867180-2343-4d44-a7a1-9cfecb127a69",
"label": "Tag",
"slug": "level-2",
},
],
"label": "Tag",
},
"title": Object {
"body": "bla",
"changed_on": null,
"created_on": "2022-02-28T12:16:31+00:00",
"format": null,
"id": "655331e9-5dcd-4fe9-8eeb-fed17d0a9155",
"label": "Text",
},
},
},
"label": "Publication",
"publish_on": Object {
"en-US": "2022-02-28T12:16:00+00:00",
},
"read_time": Object {
"en-US": 1,
},
},
Object {
"changed_on": "2022-01-06T15:01:57+00:00",
"created_on": "2022-01-06T15:01:57+00:00",
"id": "20a86707-6144-461f-9b2a-7ab27b834b79",
"items": Object {
"en-US": Object {
"active": Object {
"changed_on": null,
"created_on": "2022-01-06T15:01:57+00:00",
"id": "d18f7a7e-785c-4a0f-b7ed-e60dc3a87f58",
"label": "Boolean",
"value": "1",
},
"audience": Object {
"items": Array [
Object {
"body": "Bewoner",
"changed_on": null,
"created_on": "2022-01-06T14:42:56+00:00",
"id": "d714c04b-5e6e-45ec-b082-dfa791b44fde",
"label": "Tag",
"slug": "bewoner",
},
],
"label": "Tag",
},
"auto_increase": Object {
"changed_on": null,
"created_on": "2022-01-06T15:01:57+00:00",
"id": "c6c08428-91f0-463c-b63b-ad636104d58a",
"label": "Boolean",
"value": "1",
},
"auto_increase_level": Object {
"changed_on": null,
"created_on": "2022-01-06T15:01:57+00:00",
"id": "649fb6e6-ba05-47b5-a2d3-1224dce58c21",
"label": "Integer",
"value": 3,
},
"characteristics": Object {
"items": Array [
Object {
"body": "Man",
"changed_on": null,
"created_on": "2022-01-06T14:48:37+00:00",
"id": "5d6333be-2cee-49ce-a945-e292a4d88d1c",
"label": "Tag",
"slug": "man",
},
Object {
"body": "Vrouw",
"changed_on": null,
"created_on": "2022-01-06T14:48:37+00:00",
"id": "4ecbc422-dd5f-48d6-aa09-2180627b6f0c",
"label": "Tag",
"slug": "vrouw",
},
],
"label": "Tag",
},
"program_comment": Object {
"body": "probeersel",
"changed_on": null,
"created_on": "2022-01-06T15:01:57+00:00",
"format": null,
"id": "9d5860cd-94f6-4b99-ad82-eeddec5792f4",
"label": "Text",
},
"program_title": Object {
"body": "Test programma 1",
"changed_on": null,
"created_on": "2022-01-06T15:01:57+00:00",
"format": null,
"id": "0f82e55c-609a-4e19-b68d-42d249d950bc",
"label": "Text",
},
"routine": Object {
"items": Array [
Object {
"changed_on": "2022-01-06T15:01:02+00:00",
"created_on": "2022-01-06T15:00:46+00:00",
"id": "56e685fc-00c7-438b-93b7-ca1557dff671",
"label": "Publication",
"publish_on": Object {
"en-US": "2022-01-06T15:00:00+00:00",
},
"read_time": Object {
"en-US": 1,
},
},
Object {
"changed_on": "2022-01-06T15:00:33+00:00",
"created_on": "2022-01-06T15:00:33+00:00",
"id": "f54a9dc3-1133-4023-8c6e-634a321d22b6",
"label": "Publication",
"publish_on": Object {
"en-US": "2022-01-06T15:00:00+00:00",
},
"read_time": Object {
"en-US": 1,
},
},
],
"label": "Publication",
},
"video": Object {
"items": Array [
Object {
"changed_on": "2022-02-28T12:15:42+00:00",
"created_on": "2022-01-06T14:59:30+00:00",
"id": "a758aa90-3dc4-4382-ae59-fa43ca90eac3",
"label": "Publication",
"publish_on": Object {
"en-US": "2022-01-06T14:58:00+00:00",
},
"read_time": Object {
"en-US": 1,
},
},
],
"label": "Publication",
},
},
},
"label": "Publication",
"publish_on": Object {
"en-US": "2022-01-06T15:01:00+00:00",
},
"read_time": Object {
"en-US": 1,
},
},
Object {
"changed_on": "2022-01-06T15:01:02+00:00",
"created_on": "2022-01-06T15:00:46+00:00",
"id": "56e685fc-00c7-438b-93b7-ca1557dff671",
"items": Object {
"en-US": Object {
"title": Object {
"body": "Test routine 2",
"changed_on": null,
I really want to filter the data so I can get for example only the videos with 'niveau' > 'level 1' But i don't know how i can get the data. In the response i get 'Object' but what does 'Object' means? Currently i'm developing the application in React Native Expo and the code which i use is:
const loadVideo = async() => {
fetch('https://cdn.prepr.io/publications?fields=items{file{cdn_files}}', {
method: 'get',
headers: {
Authorization: 'Bearer {Token}'}
})
.then((r) => r.json())
.then((r) => {
console.log(r)
})
.catch((err) => {
console.log(err)
})
}

Array parsing and converting to new array List

How to convert below array to specific output?
Input:
[
{
"id": "9664581",
"isSelected": true,
"isExpanded": false,
"disabled": false,
"cells": [
{
"id": "9664581:att_name",
"value": "Test1 att_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_name"
}
},
{
"id": "9664581:att_email",
"value": "test1#gmail.com",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_email"
}
},
{
"id": "9664581:comp_name",
"value": "Test1 comp_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "comp_name"
}
},
{
"id": "9664581:attendee_ctry",
"value": "Test cnt",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "attendee_ctry"
}
},
{
"id": "9664581:sources",
"value": "Test DB",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "sources"
}
}
]
},
{
"id": "9528552",
"isSelected": true,
"isExpanded": false,
"disabled": false,
"cells": [
{
"id": "9528552:att_name",
"value": "Test2 att_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_name"
}
},
{
"id": "9528552:att_email",
"value": "Test2#gmail.com",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_email"
}
},
{
"id": "9528552:comp_name",
"value": "Dsd comp_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "comp_name"
}
},
{
"id": "9528552:attendee_ctry",
"value": "Test2 cnt",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "attendee_ctry"
}
},
{
"id": "9528552:sources",
"value": "Test2 DB",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "sources"
}
}
]
}
]
output should be like
output:
[
{
"id": "9664581",
"name": "Test1 att_name",
"email": test1#gmail.com
},
{
"id": "9528552",
"name": "Test2 att_name",
"email": test2#gmail.com
}
]
You need to understand your data.
It appears that the id comes right off of each object. As for the name and email, those are the values of the first and second cell. This turns into a trivial mapping.
You could also try to look-up the cells by their header:
function transformData(data) {
return data.map(item => {
return {
id : item.id,
name : item.cells.find(cell => cell.info.header.endsWith('name')).value,
email : item.cells.find(cell => cell.info.header.endsWith('email')).value
};
});
}
console.log(transformData(getData()));
function transformData(data) {
return data.map(item => {
return {
id : item.id,
name : item.cells[0].value,
email : item.cells[1].value
};
});
}
function getData() {
return [{
"id": "9664581",
"isSelected": true,
"isExpanded": false,
"disabled": false,
"cells": [{
"id": "9664581:att_name",
"value": "Test1 att_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_name"
}
}, {
"id": "9664581:att_email",
"value": "test1#gmail.com",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_email"
}
}, {
"id": "9664581:comp_name",
"value": "Test1 comp_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "comp_name"
}
}, {
"id": "9664581:attendee_ctry",
"value": "Test cnt",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "attendee_ctry"
}
}, {
"id": "9664581:sources",
"value": "Test DB",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "sources"
}
}]
}, {
"id": "9528552",
"isSelected": true,
"isExpanded": false,
"disabled": false,
"cells": [{
"id": "9528552:att_name",
"value": "Test2 att_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_name"
}
}, {
"id": "9528552:att_email",
"value": "Test2#gmail.com",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "att_email"
}
}, {
"id": "9528552:comp_name",
"value": "Dsd comp_name",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "comp_name"
}
}, {
"id": "9528552:attendee_ctry",
"value": "Test2 cnt",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "attendee_ctry"
}
}, {
"id": "9528552:sources",
"value": "Test2 DB",
"isEditable": false,
"isEditing": false,
"isValid": true,
"errors": null,
"info": {
"header": "sources"
}
}]
}];
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
You could do this:
let output = input.map(v => {
let name = '';
let email = '';
v.cells.map(obj => {
if(obj.info.header === 'att_name') {
name = obj.value
}
if(obj.info.header === 'att_email') {
email = obj.value
}
return
})
return {
"id": v.id,
"name": name,
"email": email
}
})

How to create a context variable in the entire multiplied-replies node in Watson Assistant?

To add a context variable in a node with a single reply, I just add the var once in that response and that's fair enough.
But in multiple replies node, I must configure each response and add the context variable in each one with either context or JSON editor.
Can I add that only once?
If not what is the idea behind that?
Why does not even a node general variable option exist besides reply-specific ones?
The multiple response node is a way to easily have different responses nested together. Before this feature you had to create a node for every single response you wanted to do.
This appears to be a side-effect of this, but is easy to resolve.
Create your main node and set your context variable. Then create a child node and have the parent "Skip User Input". The child node should have your multiple responses.
Example workspace:
{
"name": "Example single context for multiresponse node",
"intents": [],
"entities": [],
"language": "en",
"metadata": {
"api_version": {
"major_version": "v1",
"minor_version": "2017-05-26"
}
},
"description": "",
"dialog_nodes": [
{
"type": "response_condition",
"title": null,
"output": {
"generic": [
{
"values": [
{
"text": "two $example"
}
],
"response_type": "text"
}
]
},
"parent": "node_1_1532968597524",
"context": null,
"metadata": {},
"next_step": null,
"conditions": "input.text == \"2\"",
"description": null,
"dialog_node": "node_3_1532968635994",
"previous_sibling": "node_2_1532968631610"
},
{
"type": "response_condition",
"title": null,
"output": {
"generic": [
{
"values": [
{
"text": "one $example"
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": "node_1_1532968597524",
"context": null,
"metadata": {},
"next_step": null,
"conditions": "input.text == \"1\"",
"description": null,
"dialog_node": "node_2_1532968631610",
"previous_sibling": null
},
{
"type": "standard",
"title": "Enter 1 or 2 to trigger.",
"output": {},
"parent": "node_4_1532968810899",
"context": null,
"metadata": {
"_customization": {
"mcr": true
}
},
"next_step": null,
"conditions": "anything_else",
"description": null,
"dialog_node": "node_1_1532968597524",
"digress_out": "allow_all",
"previous_sibling": null
},
{
"type": "standard",
"title": "$example set to value of \"example\"",
"output": {
"generic": [
{
"values": [],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": null,
"context": {
"example": "example"
},
"metadata": {},
"next_step": {
"behavior": "skip_user_input"
},
"conditions": "anything_else",
"description": null,
"dialog_node": "node_4_1532968810899",
"previous_sibling": "Welcome"
},
{
"type": "standard",
"title": "Anything else",
"output": {
"generic": [
{
"values": [
{
"text": "I didn't understand. You can try rephrasing."
},
{
"text": "Can you reword your statement? I'm not understanding."
},
{
"text": "I didn't get your meaning."
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": null,
"context": null,
"metadata": {},
"next_step": null,
"conditions": "anything_else",
"description": null,
"dialog_node": "Anything else",
"previous_sibling": "node_4_1532968810899"
},
{
"type": "standard",
"title": "Welcome",
"output": {
"generic": [
{
"values": [
{
"text": "Hello. How can I help you?"
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": null,
"context": null,
"metadata": {},
"next_step": null,
"conditions": "welcome",
"description": null,
"dialog_node": "Welcome",
"previous_sibling": null
}
],
"workspace_id": "9e5b9851-5f0e-4e5f-9523-d2ac7045bf1d",
"counterexamples": [],
"system_settings": {
"tooling": {
"store_generic_responses": true
},
"disambiguation": {
"prompt": "Did you mean:",
"none_of_the_above_prompt": "None of the above"
},
"human_agent_assist": {
"prompt": "Did you mean:"
}
},
"learning_opt_out": false
}

jq inner array value as new key outside the array

Input:
{
"data": {
"assets": [{
"organizationId": "1056bda9-2598-4fdf-bd99-db3924464a75",
"createdAt": "2018-03-14T14:41:41.154Z",
"tags": [{
"value": "raml",
"key": null,
"mutable": false
},
{
"value": "rest",
"key": null,
"mutable": false
},
{
"value": "api",
"key": null,
"mutable": false
},
{
"value": "v1",
"key": "product-api-version",
"mutable": false
},
{
"value": "has-mule4-connector",
"key": null,
"mutable": false
},
{
"value": "has-mule3-connector",
"key": null,
"mutable": false
},
{
"value": "system",
"key": null,
"mutable": true
},
{
"value": "sourcing",
"key": null,
"mutable": true
}
],
"type": "rest-api"
},
{
"organizationId": "SASAAs",
"createdAt": "2018-03-14T14:41:41.154Z",
"tags": [{
"value": "raml",
"key": null,
"mutable": false
},
{
"value": "rest",
"key": null,
"mutable": false
},
{
"value": "api",
"key": null,
"mutable": false
},
{
"value": "v1",
"key": "product-api-version",
"mutable": false
},
{
"value": "has-mule4-connector",
"key": null,
"mutable": false
},
{
"value": "has-mule3-connector",
"key": null,
"mutable": false
},
{
"value": "system",
"key": null,
"mutable": true
},
{
"value": "supply-chain",
"key": null,
"mutable": true
}
],
"type": "rest-api"
}
]
}
}
Expected output:
{
"data": {
"assets": [{
"organizationId": "1056bda9-2598-4fdf-bd99-db3924464a75",
"createdAt": "2018-03-14T14:41:41.154Z",
"tags": [{
"value": "raml",
"key": null,
"mutable": false
},
{
"value": "rest",
"key": null,
"mutable": false
},
{
"value": "api",
"key": null,
"mutable": false
},
{
"value": "v1",
"key": "product-api-version",
"mutable": false
},
{
"value": "has-mule4-connector",
"key": null,
"mutable": false
},
{
"value": "has-mule3-connector",
"key": null,
"mutable": false
},
{
"value": "system",
"key": null,
"mutable": true
},
{
"value": "sourcing",
"key": null,
"mutable": true
}
],
"type": "rest-api",
"domain": "sourcing"
},
{
"organizationId": "SASAAs",
"createdAt": "2018-03-14T14:41:41.154Z",
"tags": [{
"value": "raml",
"key": null,
"mutable": false
},
{
"value": "rest",
"key": null,
"mutable": false
},
{
"value": "api",
"key": null,
"mutable": false
},
{
"value": "v1",
"key": "product-api-version",
"mutable": false
},
{
"value": "has-mule4-connector",
"key": null,
"mutable": false
},
{
"value": "has-mule3-connector",
"key": null,
"mutable": false
},
{
"value": "system",
"key": null,
"mutable": true
},
{
"value": "supply-chain",
"key": null,
"mutable": true
}
],
"type": "rest-api",
"domain": "supply-chain"
}
]
}
}
SO far, I tried this which worked partially for me.
.data.assets[] | select (.tags[].value=="sourcing") | . += {"domain":"sourcing"}
The problem is that I want this condition to apply for every object inside the array but I'm not able to do that. It is getting applied to the first object only.
Where am i doing wrong? Any suggestions please?
The following seems to meet the descriptive requirements:
.data.assets |=
map( if any(.tags[].value; . == "sourcing")
then . + {"domain":"sourcing"}
else .
end )
This produces the desired output except for the key-value pair "domain": "supply-chain" that is inconsistent with the descriptive requirements.
The following, by contrast, takes its cue from (that is, produces) the given output:
.data.assets |=
map( if any(.tags[].value; . == "sourcing") then . + {"domain":"sourcing"}
elif any(.tags[].value; . == "supply-chain") then . + {"domain":"supply-chain"}
else . end )
Setting "domain" to all the tag values
.data.assets |= map( .domain += [.tags[].value] )

Resources