Remove an item from step function output - json

When passing inputs from one state to another, is there a way to remove a particular Key-value from the input event?
For example:
I need to remove CurrentSubAccountChunkResults key and value from the input before passing it to the next state of the step function.
I read I could specify Parameters to filter out.
Rather than specifying all the parameters I need , I am trying to find a way to remove what I do not need in the input and pass the rest to the next state.
Could someone help me with any ideas they might have?
{
"Item1": { item: No1, item: No2 },
"Item2": "iam-item2",
"Item3": 3,
"Item4": { item: No4, item: No5 },
"CurrentSubAccountChunkResults" : { "result1": "iam-result1" },
"VerificationResults": { "final-result1": "iam-result1" }
}

I believe your options are:
A Lambda task to remove unwanted keys and values
Two pass tasks to redact values, but not the keys
Here's the non-Lambda option. The first Pass task adds the "private" key names you want to redact. The second Pass uses the JsonMerge intrinsic function to merge payload and private.
{
"StartAt": "AddPrivateFields",
"States": {
"AddPrivateFields": {
"Type": "Pass",
"Parameters": {
"payload.$": "$",
"private": {
"CurrentSubAccountChunkResults": null
}
},
"Next": "Redact"
},
"Redact": {
"Type": "Pass",
"Parameters": {
"redacted.$": "States.JsonMerge($.payload, $.private, false)"
},
"End": true
}
}
}
If the two objects in the merge function ("States.JsonMerge($.payload, $.private, false)") share keys, the values in the second will take precedence. This sets the private keys to null in the output:
{
"redacted": {
"CurrentSubAccountChunkResults": null,
"Item1": {
"item": "No1",
"item2": "No2"
},
"Item2": "iam-item2",
"Item3": 3,
"Item4": {
"item": "No4",
"item2": "No5"
},
"VerificationResults": {
"final-result1": "iam-result1"
}
}
}

Related

Can't access property from JSON file, why?

Here's the JSON:
data = {
"company_name": "חברה לדוגמה",
"audit_period_begin": "01/01/2021",
"audit_period_end": "31/12/2021",
"reports": [
{
"type": {
"he": "מאזן",
"en": "Balance Sheets"
},
"fin_statement": "BS",
"sections": [
{
"section_name": {
"he": "נכסים שוטפים",
"en": "Current Assets"
},
"totals": {
"2020": {
"final_total_local": 100000,
"final_total_foreign": 0
},
"2021": {
"final_total_local": 110000,
"final_total_foreign": 0
}
},
"subsections": [
{......(the rest is irrelevant)
and I'm trying to call:
data.reports[0].sections[0]['totals']
but I get an error:
Element implicitly has an 'any' type because expression of type '"totals"' can't be used to index type
and can't read the property, why?
After you acquire data.reports[0].sections[0].totals probably successfully you are accessing it as if it was an array, whereas it's an object. try data.reports[0].sections[0]['totals']["2020"] instead of data.reports[0].sections[0]['totals'][2020]. This is just a guess as you didn't provide enough code.

Azure CI/CD Json transformation on array

When I'm creating a Task for an Azure DevOps release, can I transform the value of an object in an array, based on the "key" of the object?
As an example, I'm copying the example Json file from this article, which is this.
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=(LocalDb)\\MSDB;AttachDbFilename=aspcore-local.mdf;"
},
"DebugMode": "enabled",
"DBAccess": {
"Administrators": ["Admin-1", "Admin-2"],
"Users": ["Vendor-1", "vendor-3"]
},
"FeatureFlags": {
"Preview": [
{
"newUI": "AllAccounts"
},
{
"NewWelcomeMessage": "Newusers"
}
]
}
}
}
What I have is something more like this, look at the "FeatureFlags" array. My array contains a list of "key"/"value" objects similar to this. I need to be able to transform the object in the array that matches a key, and have the transform process replace the "value" property value.
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Data Source=(LocalDb)\\MSDB;AttachDbFilename=aspcore-local.mdf;"
},
"DebugMode": "enabled",
"DBAccess": {
"Administrators": ["Admin-1", "Admin-2"],
"Users": ["Vendor-1", "vendor-3"]
},
"FeatureFlags": {
"Preview": [
{
"key": "newUI",
"value": "AllAccounts"
},
{
"key": "NewWelcomeMessage",
"value": "Newusers"
}
]
}
}
}
For example, I want to change the value of "Newusers" in the object with a key of NewWelcomeMessage, to "AllUsers".
In the Azure Release "Variables" tab, can I simply use this as the "Name", Data.FeatureFlags.Preview.NewWelcomeMessage, and AllUsers as the value for it to transform the value for the correct object, or will this fail?
This pattern seems to work with XML, see this.

jmespath :select json object element based on other (array) element in the object

I have this JSON
{
"srv_config": [{
"name": "db1",
"servers": ["srv1", "srv2"],
"prop": [{"source":"aa"},"destination":"bb"},{"source":"cc"},"destination":"cc"},]
}, {
"name": "db2",
"servers": ["srv2", "srv2"],
"prop": [{"source":"dd"},"destination":"dd"},{"source":"ee"},"destination":"ee"},]
}
]
}
I try to build a JMESPath expression to select the prop application in each object in the main array, but based on the existence of a string in the servers element.
To select all props, I can do:
*.props [*]
But how do I add condition that says "select only if srv1 is in servers list"?
You can use the contains function in order to filter based on a array containing something.
Given the query:
*[?contains(servers, `srv1`)].prop | [][]
This gives us:
[
{
"source": "aa",
"destination": "bb"
},
{
"source": "cc",
"destination": "cc"
}
]
Please mind that I am also using a bit of flattening here.
All this run towards a corrected version of you JSON:
{
"srv_config":[
{
"name":"db1",
"servers":[
"srv1",
"srv2"
],
"prop":[
{
"source":"aa",
"destination":"bb"
},
{
"source":"cc",
"destination":"cc"
}
]
},
{
"name":"db2",
"servers":[
"srv2",
"srv2"
],
"prop":[
{
"source":"dd",
"destination":"dd"
},
{
"source":"ee",
"destination":"ee"
}
]
}
]
}

How to add an array of objects in JSON

My question is entirely related to the structure of JSON. I've this:
{
"cars": {
"rows": [
{
"name": "Mercedes",
"color": "Black",
"make": "Mercedes"
},
{
"name": "BMW",
"color": "Black",
"make": "BMW Germany"
},
{
"name": "Innova",
"color": "Red",
"make": "Toyota"
}
]
}
}
Till now, row is an array of objects that contains information of different cars. I want two such row arrays. Only two are required. Like this:
{
"staticKpi": {
"rows": [
{
// 1st row 1st object
},
{
// 1st row 2nd object
},
{
// 1st row 3rd object
}
],
[
{
// 2nd row 1st object
},
{
// 2nd row 2nd object
},
{
// 2nd row 3rd object
}
]
}
}
You can see this JSON here.
But this is giving me JSON error. I just want to keep two lines of objects so there will be only two arrays in rows. hope I was able to explain the problem. Please correct my mistake.
PS: I've run forEach loop later on this JSON. So I've to take care of that too.
Try like this:
{
"staticKpi":{
"rows":[
[
{
"name":"Mercedes",
"color":"Black",
"make":"Mercedes"
},
{
"name":"BMW",
"color":"Black",
"make":"BMW Germany"
},
{
"name":"Innova",
"color":"Red",
"make":"Toyota"
}
],
[
{
"name":"Mercedes",
"color":"Black",
"make":"Mercedes"
},
{
"name":"BMW",
"color":"Black",
"make":"BMW Germany"
},
{
"name":"Innova",
"color":"Red",
"make":"Toyota"
}
]
]
}
}
rows needs to be one object/array, it can't be two. To have more than one element, you need to put those two elements in an array.
EDIT:
To loop through these using forEach in JavaScript, you could do this:
data = {"staticKpi":{"rows":[[{"name":"Mercedes","color":"Black","make":"Mercedes"},{"name":"BMW","color":"Black","make":"BMW Germany"},{"name":"Innova","color":"Red","make":"Toyota"}],[{"name":"Mercedes","color":"Black","make":"Mercedes"},{"name":"BMW","color":"Black","make":"BMW Germany"},{"name":"Innova","color":"Red","make":"Toyota"}]]}};
data.staticKpi.rows.forEach((row) => console.log(row));

how to add a list of JSON values into keys with JQ

I have an input file:
{
"errands": [
{
"name": "broker-deregistrar",
"label": "Deregister and Purge Instances",
"impact_warning": null,
"pre_delete": true
},
{
"name": "delete-all-service-instances",
"label": "Delete All Service Instances",
"impact_warning": null,
"pre_delete": true
},
{
"name": "deregister-broker",
"label": "Deregister On-Demand Service Broker",
"impact_warning": null,
"pre_delete": true
}
]
}
I would like to reformat this to make the values of .name into a key with a fixed value like this:
{
"deploy_products": "all",
"errands": {
"product_1_guid": {
"run_pre_delete": {
"broker-deregistrar": true,
"delete-all-service-instances": true,
"deregister-broker": true
}
}
},
"ignore_warnings": true
}
I can subset the values I want with this filter:
.errands[].name
which gives me:
"broker-deregistrar"
"delete-all-service-instances"
"deregister-broker"
but I want to get the selected values into a new JSON as keys.
while this kind of works,
.errands=(.product_1_guid=(.run_pre_delete=(.xxx=true | .yyy=true | .zzz=true)))
the list of errand names is variable in that they have different names and counts. i.e. the list of errands may only be "delete-apps", or even nothing at all.
and in the above example I need .xxx, .yyy and .zzz to come from the original JSON.
Generate the name-true pairs within an array constructor so that you can easily merge them with add and place the result wherever it belongs.
{
deploy_products: "all",
errands: {
product_1_guid: {
run_pre_delete: [
{ (.errands[].name): true }
] | add
}
},
ignore_warnings: true
}
Online demo