cancel
Showing results for 
Search instead for 
Did you mean: 

JSON STRING VALUES INTO COLUMNS

mohit_jain
New Contributor III

Hi Everyone..

I have a requirement where we have to split the json which is coming into the json but it is in String. We have to divide into the columns.

Let me explain you by the example. 
The Json is coming in this format.

[
{
"String":
"{col_name=CTRY_CODE, data_type=varchar(32768)},{col_name=SRCE_CODE, data_type=varchar(32768)},{col_name=PROD_CODE, data_type=varchar(32768)}}}"
}
]

mohit_jain_3-1692677660916.png

 

mohit_jain_0-1692677471699.png

But the result we need is :

mohit_jain_1-1692677596749.png

Can anyone help me to solve this.

Thanks in advance.

@alchemiz @j_angelevski 

 

2 REPLIES 2

Matthias
New Contributor III

I think your initial problem is that the data is not actual json.

Field separators should be ":" - and the parenteses are clearly off.

Snaplogic will still however allow you to do this (you can probably condense this into one mapper if you give it a bit more thought ...):

matthias_voppic_0-1692685128516.png

first mapper: `$String.split('},{')` 

2nd mapper: `$splitValue.replace('{', '').replaceAll('}}}', '').split(', ').toObject(x => x.split('=')[0], x => x.split('=')[1])`

 

The json splitter in between just splits the data into multiple rows.

 

matthias_voppic_2-1692685221685.png

 

 

alchemiz
Contributor III

Hi @mohit_jain,

Good day, you can also try to manipulate the string to be a valid JSON string.. just like what Sir @Matthias suggested

Expression:
JSON.parse('['.concat($.get('String','').replace(/\{|\=|\}|\,[^\{]/g, m => match m {'{'=> '{"', '='=> '":"', '}'=> '"}', ', '=> '","', ','=> '","'}),']'))

alchemiz_0-1692723129311.png