Reputation: 51
I want to convert data separated by | to json format.
There are a total of 28 columns of data and can contain blanks.
This is an example of text data.
var str = "0000|testvalue|javascript|0|20201125|20201118|0108|01|card|08|2000|66|7|0|0|1927|string|201118000001|name|||01||00|48758984|0|||
0000|testvalue|javascript|2|20201125|20201118|0108|01|card|08|-1000|-33|-3|0|0|-964|string|201118000001|name|||01||00|48758984|0|||
0000|testvalue|javascript|2|20201125|20201118|0108|01|card|08|-1000|-33|-3|0|0|-964|string|201118000001|name|||01||00|48758984|0|||
0000|testvalue|javascript|0|20201125|20201118|08|03|cup|01|1000|300|30|0|0|670|string|201118000012|name||||||||020||28275319918925"
I want to change it to a json array like this.
var json_array = [
{m1 : "0000", m2 : "testvalue", m3 : "javascript", m4 : "0", m5 : "20201125", m6 : "20201118", m7 : "0108", m8 : "01", m9 : "card", .................},
{m1 : "0000", m2 : "testvalue", m3 : "javascript", m4 : "2", m5 : "20201125", m6 : "20201118", m7 : "0108", m8 : "01", m9 : "card", .................},
{m1 : "0000", m2 : "testvalue", m3 : "javascript", m4 : "2", m5 : "20201125", m6 : "20201118", m7 : "0108", m8 : "01", m9 : "card", .................},
{m1 : "0000", m2 : "testvalue", m3 : "javascript", m4 : "0", m5 : "20201125", m6 : "20201118", m7 : "08", m8 : "01", m9 : "cup", .................}
]
It is difficult to process textual data, leaving questions. Thank you for your kind answer.
Upvotes: 0
Views: 211
Reputation: 25
Is there any |
in your data?
If there is not, you could just split the data by scanning the |
—— you have known that there are 28 columns in total.
If there are ... Could you guarantee that the input is unambiguous? Maybe you could write a regex to detect rows.
Upvotes: 0
Reputation: 7464
It looks like your string has rows, separated by \n
(or possibly \r\n
, if it's from a windows system?). And then columns, separated by the |
character. To split this into a two dimensional array, you can use the following:
str.split(/\r?\n/).map(x => x.split('|'));
The first split creates a 1d array of rows. The .map
tells it to look at each row, and split on the column delimiter.
The next step is to change the row data from an array to an object. This can be accomplished by adding a .reduce
to the row data:
var str = `0000|testvalue|javascript|0|20201125|20201118|0108|01|card|08|2000|66|7|0|0|1927|string|201118000001|name|||01||00|48758984|0|||
0000|testvalue|javascript|2|20201125|20201118|0108|01|card|08|-1000|-33|-3|0|0|-964|string|201118000001|name|||01||00|48758984|0|||
0000|testvalue|javascript|2|20201125|20201118|0108|01|card|08|-1000|-33|-3|0|0|-964|string|201118000001|name|||01||00|48758984|0|||
0000|testvalue|javascript|0|20201125|20201118|08|03|cup|01|1000|300|30|0|0|670|string|201118000012|name||||||||020||28275319918925`;
var result = str.split(/\r?\n/).map(x => x.split('|').reduce((a, o, i) => {
a['m' + (i+1)] = o;
return a;
}, {}));
console.log(JSON.stringify(result, null, 2));
Upvotes: 2