Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
See why IDC MarketScape names Qlik a 2025 Leader! Read more
cancel
Showing results for 
Search instead for 
Did you mean: 
BooWseR
Contributor
Contributor

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

Labels (4)
1 Solution

Accepted Solutions
TRF
Champion II
Champion II

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

0683p000009M3Lk.png

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

0683p000009M3Y9.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.

 

 

 

View solution in original post

6 Replies
TRF
Champion II
Champion II

Can share the expected result based on your input?
BooWseR
Contributor
Contributor
Author

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.
TRF
Champion II
Champion II

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

0683p000009M3Lk.png

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

0683p000009M3Y9.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.

 

 

 

BooWseR
Contributor
Contributor
Author

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 0683p000009MACn.png

TRF
Champion II
Champion II

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

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

BooWseR
Contributor
Contributor
Author

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?