Replace value according to its poition

Below is the Input
[
{
@id”: “2”,
“col”: [
{
@id”: “2”,
@transable”: “yes”,
“$”: “Replace1”
},
{
@id”: “3”,
@transable”: “no”,
“$”: “Replace2”
}
],
“group”: [
{
“A”: “A1”,
“B”: “B1”,
“C”: “C1”,
“D”: “D1”,
“E”: "E1 ",
“F”: “F1”,
“G”: “G1”,
“H”: “”,
“I”: “”
},
{
“A”: “A2”,
“B”: “B2”,
“C”: “C2”,
“D”: “D2”,
“E”: “”,
“F”: “F2”,
“G”: “G2”,
“H”: “”,
“I”: “”
}
]
}
]


expected Output :

[
  {
    "group": [
      {
        "A": "A1",
        "B": "B1",
        "C": "C1",
        "D": "D1",
        "E": "E1 ",
        "F": "F1",
        "G": "G1",
        "H": "",
        "I": ""
      },
      {
        "A": "A2",
        "B": "Replace1",
        "C": "Replace2",
        "D": "D2",
        "E": "",
        "F": "F2",
        "G": "G2",
        "H": "",
        "I": ""
      }
         ]
  }
]

explanantion : @id refers to row number of the group and @id under col refers to the column number of the group where replacement needs to be done with  the value  of "$"

Hi @KTsnap,

I believe this expression will do the job:

$group.map((x,index)=>$['@id'] == index + 1 ? (x.mapValues((v,k)=> $col.find(e => e['@id']-1 == x.keys().indexOf(k)) != null ? $col.find(e => e['@id']-1 == x.keys().indexOf(k) ).get('$') : v)) : x)

It’s rather complex, so I would suggest you to do some testing.

Regards,
Bojan

3 Likes

Hi @bojanvelevski similarly if i have two array called row and group as shown below
inputcommunity



I need to replace the the value of second array ie group array with values from first array ie row array
to elaborate->row id in first array (row array) is corresponding to first record in second array(group array) and colid in first array(row array) is corresponding to A, B, C,D,E,F,G,H,I in the second array(group array).Example:first record of second array(group array) with values corresponding to column B and C must be updated with value ($) corresponding to first array(row array) with row id 1 and col id is 2 &col id is 3but remaining column values like A,D,E,F…I values should remain as incoming input values

Expected output is as shown below

Please could u help telling how can we achieve this using mapper expression here

If my understanding is correct, this is the same situation, isn’t it ?

You just need to tweak the expression a little bit.

Hi @bojanvelevski yes but here id corresponding to row comes in an array so how can we change it accordingly?
i tried this way its not working pls could u help if am missing something or doing wrong pls


input.json (1.0 KB)
@bojanvelevski, @alchemiz please could u help :pensive:

Hi @bojanvelevski , @alchemiz if i try hardcoding the record 1 , record2 in the expression i am getting as expected but how can we dynamically loop across all record?

You’re trying to update the value from $row right?

$row.map(r=> r.mapValues((val,key)=> key == ‘col’ ? val.map(c=> c.mapValues((v,k)=> k == ‘$’ ? $group[r[‘@id’] - 1].values()[c[‘@id’] - 1] : v)) : val))

Hi @alchemiz its not my requirement actually i had listed my requirement above i am updating value obtained from row array to group array

$group.map((g,i)=> $row.find((a,b,c)=> a[‘@id’] == i + 1) instanceof Object ? g.mapValues((val,key)=> $row.find((a,b,c)=> a[‘@id’] == i + 1)[‘col’].find((x,y,z)=> x[‘@id’] == (key.charCodeAt(0) - 64)) instanceof Object ? $row.find((a,b,c)=> a[‘@id’] == i + 1)[‘col’].find((x,y,z)=> x[‘@id’] == (key.charCodeAt(0) - 64))[‘$’] : val ) : g )

hi @alchemiz thanks for helping but thats not exact requirement actually there are two arrays in input called row and group
inputcommunity
and each looks like this as shown below



so according to position values (rowid & colid) need to update values of second array (group array)only
expected output:

My bad, I got the charCode wrong it should be 64 :smiley:

$group.map((g,i)=> $row.find((a,b,c)=> a[‘@id’] == i + 1) instanceof Object ? g.mapValues((val,key)=> $row.find((a,b,c)=> a[‘@id’] == i + 1)[‘col’].find((x,y,z)=> x[‘@id’] == (key.charCodeAt(0) - 64)) instanceof Object ? $row.find((a,b,c)=> a[‘@id’] == i + 1)[‘col’].find((x,y,z)=> x[‘@id’] == (key.charCodeAt(0) - 64))[‘$’] : val ) : g )

hi @alchemiz the column keys in group array maynot be characters like A,B,C… its suppose to map based on the position like col1,col2 etc the expression suggested by @bojanvelevski
$group.map((x,index)=>$[‘@id’] == index + 1 ? (x.mapValues((v,k)=> $col.find(e => e[‘@id’]-1 == x.keys().indexOf(k)) != null ? $col.find(e => e[‘@id’]-1 == x.keys().indexOf(k) ).get(‘$’) : v)) : x)
works irrespective of column name in group array but its should be tweaked to satisfy my input becoz in which he had metioned $[‘@id’] comes in as filed but in my case it comes in as an array element like this jsonPath($, “row[][‘@id’]") and also $col comes in as an array but in my case it comes in under row array like this jsonPath($, "row[].col”), so i need help in adjusting the above expression to satisfy my input.Please could u help :pensive:

Just replace the criteria when checking for the col object…

$group.map((g,i)=> $row.find((a,b,c)=> a[‘@id’] == i + 1) instanceof Object ? g.mapValues((val,key)=> $row.find((a,b,c)=> a[‘@id’] == i + 1)[‘col’].find((x,y,z)=> x[‘@id’] == (g.keys().indexOf(key) + 1)) instanceof Object ? $row.find((a,b,c)=> a[‘@id’] == i + 1)[‘col’].find((x,y,z)=> x[‘@id’] == (g.keys().indexOf(key) + 1)).get(‘$’,val) : val ) : g )

@alchemiz thanks a lot!! its working :slight_smile:

Glad to be of help :metal:

1 Like