Split String into Key / Value

Five Stars

Split String into Key / Value

Hi guys,

I have a long string that I'd like to split into key values:

 

# Key1: Value1\n# Key2: Value2\n# Key3: Value3...

I split the string with a tNormalize and removed the \n with a tJavaRow, but how do I get the values written as Key / Value in a JSON? I tried before to convert the string into an array and use tMap to map part[0] and part[1] into a schema, but i can't use tMap with an array. In addition, there isn't always a key. Some points consist only of values:

 

 

# Key3: Value3\n# Value\n# Key4: Value4\n...

To use Javacode to write the key/values directly into a JSON file isnt realy an option, cause the data have to combine with other data later.

 

If anyone had an idea, I'd really appreciate it. I saw that there was already another topic here in the forum, but I failed a bit because of the complexity and I think it's more than I need.

 

Best regards


Accepted Solutions
Highlighted
Fifteen Stars TRF
Fifteen Stars

Re: Split String into Key / Value

Based on your sample, I used a tNormalize with "\n" for the separator:

img1.png

Then the magic is with the tMap wich allow to separate keys and values using an array (yes, you can):

img2.png

Here are the expressions to get the keys and values (when no key, a key name is generated).

Keys :

((String[])Var.var1).length > 1 ? ((String[])Var.var1)[0] : "newKey" + Numeric.sequence("newKey", 1, 1) 

Values:

((String[])Var.var1).length > 1 ? ((String[])Var.var1)[1] : ((String[])Var.var1)[0] 

And the result is:

[statistics] connecting to socket on port 3609
[statistics] connected
.-------+------.
|  tLogRow_10  |
|=------+-----=|
|key    |value |
|=------+-----=|
|Key1   |Value1|
|Key2   |Value2|
|Key3   |Value3|
|newKey1|Value |
|Key4   |Value4|
'-------+------'

[statistics] disconnected

(see the name "newKey1" when key is not in the input string).

Hope this helps.

 

 

 


TRF

All Replies
Fifteen Stars TRF
Fifteen Stars

Re: Split String into Key / Value

Can share the expected result based on your input?

TRF
Five Stars

Re: Split String into Key / Value

Thanks for the quick response.

My final result should be like:
<product_description>
<key1>Value1</key1>
<key2>Value2</key2>
....
</product_description>
 
Like i said, these data are only a part of the job, so i have to write the JSON later. For the beginning, it would be great if I could handle the data as separated as possible. If Key and Value were in separate fields, I could assign the data in tMap as I want.
The problem is that the final structure is not completely finished yet, but I would like to handle the data as separated as possible.
 
I hope you know what I mean.
Highlighted
Fifteen Stars TRF
Fifteen Stars

Re: Split String into Key / Value

Based on your sample, I used a tNormalize with "\n" for the separator:

img1.png

Then the magic is with the tMap wich allow to separate keys and values using an array (yes, you can):

img2.png

Here are the expressions to get the keys and values (when no key, a key name is generated).

Keys :

((String[])Var.var1).length > 1 ? ((String[])Var.var1)[0] : "newKey" + Numeric.sequence("newKey", 1, 1) 

Values:

((String[])Var.var1).length > 1 ? ((String[])Var.var1)[1] : ((String[])Var.var1)[0] 

And the result is:

[statistics] connecting to socket on port 3609
[statistics] connected
.-------+------.
|  tLogRow_10  |
|=------+-----=|
|key    |value |
|=------+-----=|
|Key1   |Value1|
|Key2   |Value2|
|Key3   |Value3|
|newKey1|Value |
|Key4   |Value4|
'-------+------'

[statistics] disconnected

(see the name "newKey1" when key is not in the input string).

Hope this helps.

 

 

 


TRF
Five Stars

Re: Split String into Key / Value

That's brilliant! Thank you very much for that.


But what doesn't work for me is the tNormalize with the "\n" separator. Here I have to split to # and then replace the \n in tMap. It works too.

 

Best regards Smiley Happy

Fifteen Stars TRF
Fifteen Stars

Re: Split String into Key / Value

Maybe you have a "\r\n" instead of a "\n" in your input.

If it comes from a text file, this is possible.


TRF
Five Stars

Re: Split String into Key / Value

Hm. The data comes from a JSON file. When I look in with Notepad++, I see "\\n". So yeah, maybe.

But another question: In a few places I have the case that I have a key but no value. This causes the key to be mistakenly recognized as a value. I thought about splitting the string after ": ", because there is always a space between colon and value. If the key has no value, there is no space after the colon.

(((String[])Var.var1).length > 1 || ((String[])Var.var1)[0].contains(":")) ? ...

So I get the key into the key field, but I don't get it out of the value field. Any idea?

What’s New for Talend Spring ’19

Watch the recorded webinar!

Watch Now

Agile Data lakes & Analytics

Accelerate your data lake projects with an agile approach

Watch

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.

Download

Tutorial

Introduction to Talend Open Studio for Data Integration.

Watch