Transform strin to json

Hello !

I need to transform this string (with carriage return)

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//example.fr //NONSGML My Product//FR
BEGIN:VEVENT
UID:Z2HOOON14VOH09RIACY3IL3C
SUMMARY:Test API NextCloud
LOCATION:Paris
DTSTART:20230108T200000Z
DTEND:20230108T210000Z
DTSTAMP:20230108T110000Z
DESCRIPTION:Ceci est un test !

To a json like that :
[
{
“BEGIN”: “VCALENDAR”,
“VERSION”: “2.0”,
and so on…
},
]

I use a n8n on version 0.136 (can’t update for the moment due to node SuiteCRM)

You could do something like that:

return $input.first().json.data.split('\r').reduce((accumulator, currentValue) => {
  const stringParts = currentValue.split(':');
  if (stringParts.slice(0).length > 1) {
    accumulator[stringParts.slice(0)[0]] = stringParts.slice(1).join(':');    
  }
  return accumulator;
}, {});

in the Code-Node. But considering that you are using an incredible old n8n version, the following would work in the Function-Node:

return items[0].json.data.split('\r').reduce((accumulator, currentValue) => {
  const stringParts = currentValue.split(':');
  if (stringParts.slice(0).length > 1) {
    accumulator[stringParts.slice(0)[0]] = stringParts.slice(1).join(':');    
  }
  return accumulator;
}, {});

Hello Jan,
your reponses are always a pleasure !

first : yes, i know i have to migrate. But i’ve built worflows with SuiteCRM nodes which is not compatible with newer versions (as far as i know). and i have some big jobs to migrate to http node using SuiteCRM native API’s

second : i have tested quickly your code. It’s not directily ok, but i will have a look later.

The code is fine, that it does not work out of the box is expected as you did not provide the name of the key the string is saved in. So I simply assumed it is called “data”. So you either have to make sure it ends up in “data” or you change “data” in the code to whatever it is actually called.

Thank you Jan for your answer. Sure the code is good, and it’s surely only some corrections on my side.
Maybe i should have been more precise on the input which is exactly that :

[
{
"data": "BEGIN:VCALENDAR VERSION:2.0 PRODID:DAVx5/4.2.6-gplay ical4j/3.2.5 (com.samsung.android.calendar) BEGIN:VEVENT DTSTAMP:20230109T204019Z UID:Z2HOOON14VOH09RIACY3IL3D SEQUENCE:1 SUMMARY:Test API NextCloud success ! LOCATION:Paris DESCRIPTION:Ceci est un test ! DTSTART:20230108T200000Z DTEND:20230108T210000Z CLASS:PUBLIC END:VEVENT END:VCALENDAR "
}
]

But i only want to be able to use data of the input with other nodes. Maybe there could be other ways ?

Hey @MPO,

Looking at the example you posted and the snippet from Jan it looks to work.

From here you should be able to use {{ $json["SUMMARY"] }} in the next node to use the value, If this isn’t the case could you share some screenshots maybe to show where the issue with the workflow?

Thanks @Jon and @jan
I’m not totaly in, but with the code of my workflow behind, maybe you should tell me where is my mistake ?

I got this error : ERROR: Always an Array of items has to be returned!

The issue seems to be that you said the different lines are separated by a carriage return, but in your posted example, they are separated by nothing.

Thank you @Jon
It’s the native response of a http GET from a nextcloud event.
In my test i had carriage return, but you’re right, there are none now. (don’t know why…)
I need to investigate further. I’ll tell you.