0

I am trying to parse a string with a following format

"value1=name1:value2=test1,value1=name2:value2=null,value1=null:value2=test3"

I would want this to be parsed as

[{ value1: "name1", value2: "test1"}, { value1: "name2", value2: "NA"},{ value1: "NA", value2: "test3"}]

If in case any value post splititng is null , just make is "NA". Since I am using split the null value is being coerced to string. Hence setting "NA" using nullish coalescing(??) is not working and hence it still returns null and not NA. It would also be helpful , If any body can suggest a simpler way to achieve this

Code that I have tried.

let input = "value1=name1:value2=test1,value1=name2:value2=null,value1=null:value2=test3";
let items = input.split(",");
let pairs = items.map((item) => {
 let splitItems = item.split(":");
 let value1 = splitItems[0].split("=")?? "NA";;
 let value2 = splitItems[1].split("=") ?? "NA";
 return { value1, value2 };
});
asked Sep 8, 2020 at 16:44
1
  • You can filter out matches containing null values. Commented Sep 8, 2020 at 16:48

4 Answers 4

3

You could use the ternary operator

const str =
 "value1=name1:value2=test1,value1=name2:value2=test2,value1=null:value2=test3"
const res = str.split(",").map((el) =>
 el.split(":").reduce((acc, pair) => {
 const [key, value = "NA"] = pair.split("=")
 return {
 ...acc,
 [key]: value === "null" ? "NA" : value,
 }
 }, {})
)
console.log(res)

answered Sep 8, 2020 at 16:52
Sign up to request clarification or add additional context in comments.

2 Comments

In case I want it to be parsed as { "SomeVal": "name1", "SomeVal2": "test1"}. rather than taking keys from input, I want a different one in the result?
@li97 then you could change the property as you want, like { [buildKey(key)]: ... } and custom your expected key with the key taken from input
0

You may try using this:

(string || '').split("=") || "NA"; 
Praveen Kumar Purushothaman
167k27 gold badges215 silver badges261 bronze badges
answered Sep 8, 2020 at 16:49

1 Comment

Step 1. We replace null with empty string step2. We chose splitted strinh or na
0

What about using Object.fromentries since your split shows two values in the format [],[].

let input = "value1=name1:value2=test1,value1=name2:value2=test2,value1=null:value2=test3";
let items = input.replace(/null/g, 'NA').split(",");
let pairs = items.map((item) => {
 let splitItems = item.split(":");
 let value5 = splitItems[0].split("=");
 let value6 = splitItems[1].split("=");
 return Object.fromEntries([value5,value6])
});
console.log(pairs)

answered Sep 8, 2020 at 17:25

Comments

0

maybe that is not a simpler but easy and saver way to parse your string, and it is versatile enough to provide key value pairs as many where provided

const input = "value1=name1:value2=test1:value3=name3:value4=test4,value1=name2:value2=test2,value1=null:value2=test3";
const mappedKey = {
 value3: 'someNewKey1',
 value4: 'someNewKey2',
};
const parseData = (input) => {
 // replace directly all null values before splitting
 const stream = input.replace(/null/g, 'NA');
 // now split by comma and equal sign
 const parsedStream = stream.split(",").map((fragment) => {
 // build a key value object, to be able to collect n value pairs
 const obj = {};
 // split groups by -> :
 const values = fragment.split(':').map((split) => {
 // split key value pairs by -> =
 return split.split('=');
 });
 // collect the key value pais into obj
 values.forEach((data) => {
 const [key, value] = data;
 obj[mappedKey[key] || key] = value;
 });
 // return the final result
 return obj;
 });
 
 return parsedStream;
}
window.onload = () => {
 document.querySelector('.input').innerHTML = input;
 document.querySelector('.result').innerHTML = JSON.stringify(parseData(input), null, 2);
}
<h3>Input:</h3>
<pre class="input"></pre>
<hr />
<h3>Parsed result:</h3>
<pre class="result"></pre>

answered Sep 8, 2020 at 17:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.