I’m using json2csv in JavaScript.
I have a json that looks like this:
Obj:{
Key1:[“a”,”b”]
}
When I call json2csv.parse function the result is two columns. I guess it happens because the comma is the default delimiter.
I would like to keep a single column of Key1 with the array items in the same column.
I tried using transform:{unshift} but it does the opposite.
Any ideas?
Related
im new to js, have two question about how to parse a csv, i have a simple csv with two column, column1:'user', column2:'amount'
how can i get one array with all the 'user' column value and one
array with all the 'amount' column value?
And can i loop trough the rows and use index to get the
value of the two column? something like csv['amount'][0] or csv[0][1] or something like this?
ty.
First, declare 2 arrays, one for user, one for amount.
And then read this post to know how to read a file line by line.
After that, read this post to know how to split a string with a comma separator.
Finally, use the array.push() method to push the data into an array.
Let's jump straight to an example code:
create table test_json_table
(
data json not null
);
I can insert to the table like this:
const columns = { data: "{ some_json: 123 }" }; // notice that the data column is passed as string
await knex('test_json_table').insert(columns);
And get data from the table like this:
await knex('test_json_table').select();
// returns:
// [
// { data: { some_json: 123 } } // notice that the data is returned as parsed JavaScript object (not a string)
// ]
When inserting a row the JSON column needs to be passed as a serialised string. When retrieving the row, an already parsed object is returned.
This is creating quite a mess in the project. We are using TypeScript and would like to have the same type for inserts as for selects, but this makes it impossible. It'd be fine to either always have string or always object.
I found this topic being discussed at other places, so it looks like I am not alone in this (link, link). It seems like there is no way to convert the object to string automatically. Or I am missing something?
It'd be nice if knex provided a hook where we could manually serialise the object into string when inserting.
What would be the easiest way to achieve that? Is there any lightweight ORM with support for that? Or any other option?
You could try objection.js that allows you to declare certain columns to be marked as json attributes and those should be stringified automatically when inserting / updating their values https://vincit.github.io/objection.js/api/model/static-properties.html#static-jsonattributes
I haven't tried if it works with mysql though. I don't see any reason why it wouldn't.
I think the easiest way using jsonb data type. mysql json type
We prefer postgresql for this kind of problem at office, easier and solid database for your problem.
Well you could call your own function before inserting that converts all objects to string and call it every time before you insert.
You can probably wrap knex to do it automatically as well.
In my Angular app I'm returning results via an API call, and allowing users to filter those results through a series of checkbox selections. Right now I'm running into an issue where, while results are returned as expected when one value is sent for a certain filter, when multiple values are selected (like filtering by more than one zipcode, for instance) I get zero results printed to the view. No errors, just no results.
After scratching my head for a while, using Chrome devtools network tab, I finally determined that the problem is that rather than wrapping each item in quotes in the payload - like this: "90001", "90002", what's being sent is this: "90001, 90002". In other words, quotes are wrapped around as if it were one value, not two.
This is the code, where I'm using a "join" to put together the values that are selected:
this.sendZipcode.emit(this.zipcodeFilters.text = arr.length === 0 ? undefined : arr.join(','));
I'm not sure how I can adjust the way this "join" is constructed, or find some other solution instead of "join", in order to have each item wrapped in quotes, rather than wrapped like one long string.
FYI, this is what I see in the network tab of Chrome devtools after selecting two zipcodes. As I explained, it's wrapped like one string, rather than as two values:
addresses.zipCode: {$in: ["90001, 90002"]}
Array.prototype.join will return a string. So you are converting your array to a single string which is being sent as a string. You want to send an array. Simply remove the join call and return arr directly.
String: "value, value"
Array: "value", "value"
I have a jQuery sortable with 3 list items with the following ID's
id_1
id_2
id_3
This gets sorted by the user and serialized using the following code
var order = $("#rank").sortable('serialize');
saveResponses(order);
and printed. Which looks like this...
id[]=1&id[]=3&id[]=2
So I've got a couple of questions...
Why does the underscore get converted to "[]="
Is there a strait forward way to get an array of the original ID's? I mean without just doing a string split, and replacing the characters?
serialize converts the data into a query string. The data is formatted (converted to an array) so you can use it in a URL as a query string (GET data).
You probably want toArray:
var order = $("#rank").sortable('toArray');
I have a table (in a RethinkDB database) that has a bunch of documents with the field VIN0. This field almost always stores numbers, as intended.
I had some data corruption recently where there are some strings in place of numbers for the field VIN0. Queries I was using to manipulate this data now return the error: "e: Expected type NUMBER but found STRING in:"
I'd like to filter for the strings, but I can't seem to find them. Is there a way to use something like Number.isInteger() to filter out these items within RethinkDB?
Thanks!
You can use typeOf to find type of a field, or a element. Let's say you want to filter document with VIN0 is a number
r.db('db').table('table').filter(r.row('VIN0').typeOf().eq('NUMBER'))
You can also try to correct problem by using coereTo to convert string to number.
r.db('db').table('table')
.filter(r.row('VIN0').typeOf().eq('STRING'))
.update({VIN0: r.row('VIN0').coerceTo('NUMBER')})
Hope this helps.