Get a nested json value of a single source field in transform map

Richard Tamparo
Mega Guru

Hello guys! I have another question. I tried searching for it already but can't find the answer so I'm posting it here. 

I will try to explain it in as detail as possible. Currently, I'm making a Transform Map based on JSON Data.  At this moment, I already know that writing a script is the Import side but I do not know what to write. What I need to do is to use a single source field for multiple target fields.

For example the value of source field "use_choicelist" are:

"data14333":[
  {
  "innerNo":"1",
  "value":"Use"
  },

  {
  "innerNo":"2",
  "value":"Dont Use"
  },

  {
  "innerNo":"3",
  "value":"Dont Use"
  },

  {
  "innerNo":"4",
  "value":"Use"
  },

  {
  "innerNo":"5",
  "value":"Dont Use"
  }
]

   

What I need to do is to use the data above to insert into different target fields.

                                       Transform Map

Import Table                                                                  Order Table

use_choicelist   <-----------------------------------------------> use_choice1   (value: Use)

use_choicelist   <-----------------------------------------------> use_choice2   (value: Dont Use)

use_choicelist   <-----------------------------------------------> use_choice3   (value: Dont Use)

use_choicelist   <-----------------------------------------------> use_choice4   (value: Use)

use_choicelist   <-----------------------------------------------> use_choice5   (value: Dont Use)

 

I hope you guys will help me. 

1 ACCEPTED SOLUTION

Richard Tamparo
Mega Guru

Thank you to 

shloke04 (congrats for becoming an MVP. Hoping I can be one in the future.)
Sourabh
 
I already fixed it thanks to you guys. 
 
var result;
var obj = JSON.parse(source.change_use_of_module);

for (var i = 0; i < obj.length; i++) {
if (obj[i].innerNo == "4") {
return result = obj[i].value;
}
}

View solution in original post

8 REPLIES 8

Sourabh26
Giga Guru

Hi,

 

In this case you just simply need to create Field Map Script as below.

 You can see below in the field map open it and configure the script as written below. 

Note : Select Use Source Script check box as true.

find_real_file.png

 

var result;
var choiceJSON = JSON.parse(source.use_choicelist); //Converting use_choicelist as JSON object.

//Check for each innerNo value
for(var filed in choiceJSON){
  //for table use_choice1
  if(choiceJSON.filed.innerNo == '1'){
    result = choiceJSON.filed.value;
    target.FIELD_NAME = result; //Use the target table field
  }
  //Similarly you can check for all other values and do the modification as per your need.
}

 

Mark this as Helpful, if Applicable.

 

Regards,

Sourabh

Thank you so much for the reply and Im sorry for the late reply as I was trying to fix how it can work. it says undefined.

Cannot read property "value" from undefined

 

shloke04
Kilo Patron

Hi,

Write a On After Transform Script and use the script below to map this JSON object to different target field of your.

Script to be used in On After Transform script:

var str = source.FIeldNAME;
var parsed = JSON.parse(str);
var getParsedLength = parsed.length;
for(var i=0;i<getParsedLength;i++){
if(getParsedLength[i].innerNo == '1'){
target.fieldName = getParsedLength[i].value;
}else if(getParsedLength[i].innerNo == '2'){
target.fieldName = getParsedLength[i].value;
}
target.update();
}

This way you can check for each Inner Number and map it to different Target field with the values you need.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Thank you so much for the reply and Im sorry for the late reply.

 

If Im gonna add this, do I have to add a script field map?