Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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
Based on your sample, I used a tNormalize with "\n" for the separator:
Then the magic is with the tMap wich allow to separate keys and values using an array (yes, you can):
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.
Thanks for the quick response.
<product_description> <key1>Value1</key1> <key2>Value2</key2> .... </product_description>
Based on your sample, I used a tNormalize with "\n" for the separator:
Then the magic is with the tMap wich allow to separate keys and values using an array (yes, you can):
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.
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
Maybe you have a "\r\n" instead of a "\n" in your input.
If it comes from a text file, this is possible.
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?