When I call:
db.any('SELECT (col1, col2, col3) FROM myTable....[other conditions]')
where db is an instance of pg-promise connection, as a promise result for
.then(function(data)) {
I get an array with objects like { row: '(ans1,ans2,ans3)' }
It is also written in pg-promise documentation
Is there any convenient way (i.e. without string triming and coma splitting) to parse it to JS object? I would like to use it in views (.pug files), for example element.col1 which will print ans1.
By wrapping column names into (), you are specifically requesting exactly what you are getting back.
Without () you will get an array of JSON objects.
Related
So I have to go through a bunch of code to get some data from an iframe. the iframe has a lot of data but in there is an object called '_name'. the first key of name is 'extension_id' and its value is a big long string. the json object is enclosed in apostrophes. I have tried removing the apostrophes but still instead of 'extension_id_output' I get a single curly bracket. the json object looks something like this
Frame {
...
...
_name: '{"extension_id":"a big huge string that I need"} "a bunch of other stuff":"this is a valid json object as confirmed by jsonlint", "globalOptions":{"crev":"1.2.50"}}}'
}
it's a whole big ugly paragraph but I really just need the extension_id. so this is the code I'm currently using after attempt 100 or whatever.
var frames = await page.frames();
// I'm using puppeteer for this part but I don't think that's relevant overall.
var thing = frames[1]._name;
console.log(frames[1])
// console.log(thing)
thing.replace(/'/g, '"')
// this is to remove the apostrophes from the outside of the object. I thought that would change things before. it does not. still outputs a single {
JSON.parse(thing)
console.log(thing[0])
instead of getting a big huge string that I need or whatever is written in extension_id. I get a {. that's it. I think that is because the whole object starts with a curly bracket. this is confirmed to me because console.log(thing[2]) prints e. so what's going on? jsonlint says this is a valid json object but maybe it's just a big string and I should be doing some kind of split to grab whaat's between the first : and the first ,. I'm really not sure.
For two reasons:
object[0] doesn't return the value an object's "first property", it returns the value of the property with the name "0", if any (there probably isn't in your object); and
Because it's JSON, and when you're dealing with JSON in JavaScript code, you are by definition dealing with a string. (More here.) If you want to deal with the object that the JSON describes, parse it.
Here's an example of parsing it and getting the value of the extension_id property from it:
const parsed = JSON.parse(frames[1]._name);
console.log(parsed.extension_id); // The ID
I am trying to write a postgres query (executed in nodejs using a pool created using the node-postgres package) that will insert a new row in a table. One of the columns in this table is of type text[]. My code is as follows:
pool.query('INSERT INTO paragraphs (lines) VALUES (ARRAY[$1]::TEXT[]) RETURNING id', [my_array], (err, results) => {
if (err) {
reject(err)
return;
}
resolve(results.rows[0].id)
})
paragraphs is the name of my table and lines is the name of the column of type text[]. my_array is a list of strings. My problem is that what is inserted is not an array of strings, but rather a single string formatted like an array. e.x.:
{"[\"First line\", \"Second line\", \"Third Line\"]"}
I want it to be:
{"First line", "Second line", "Third Line"}
I have also tried taking out the ARRAY and TEXT parts (so the sql looks like INSERT INTO paragraphs (lines) VALUES ($1) RETURNING id in the above query), but then I receive the errors:
malformed array literal: "["New First line", "Second line", "Third Line"]"
DETAIL: "[" must introduce explicitly-specified array dimensions.
What is the correct way to insert lists of strings into PostgreSQL tables in nodejs through a query executed by a pool?
According to: https://node-postgres.com/features/queries#parameterized-query
Parameters passed as the second argument to query() will be converted to raw data types using the following rules:
...
Array
Converted to a string that describes a Postgres array. Each array item is recursively converted using the rules described here.
So:
VALUES ($1) or VALUES ($1::TEXT[])should suffice.
I have a very trivial problem, and I'm having trouble finding similar questions. On my Node JS server, I prepare an object of key-value pairs. The keys that have spaces in them are converted to strings like this {'key':'value'}. However, the keys without spaces or special characters don't have quotes surrounding them. When I print it out it looks like this {key:'value'}. The problem is, when I send the response back to the client, the keys without surrounding quotes are missing from the object. So how would I surround all the keys with quotes then so that it is sent properly?
JSON Objects must follow the RFC-7159, the easiest way to get a RFC-compliant JSON object is to use JSON.stringify on the object you want to output on your server side, which is natively supported in NodeJS.
You just need to convert the response to client by converting the javascript object to JSON object.
var object = { key: 'value' };
var newObject = JSON.stringify(object);
console.log("format : ", newObject);
//the JSON.stringify() function definition, it will simply turn the object log into a JSON string.
OUTPUT:
format : '{ 'key' : 'value' }'
I have the following code:
def light():
result = collection1.find({"deviceName": })
lights_id = []
for x in result:
lights_id.append(x["_id"])
return lights_id
I need to fetch in a database for a device name containing the string light, and I need to fill it in the blank but I don't know how to do it in this case.
Assuming your collecyion1 is a mongodb collection, i think this should work
collection1.find({'deviceName': {'$regex': ".*light.*", '$options': 'i'}})
as mongodb supports regex, we are searching a text using regex in this case
./*light.* and passing option i so that the match is case insensetive.
This appears to be pretty basic but I am unable to find a suitable pipeline expression function to achieve this.
I have set an array variable VAR1 with the following value, which is an output from a SQL Lookup activity in an ADF pipeline:
[
{
"Code1": "1312312"
},
{
"Code1": "3524355"
}
]
Now, I need to convert this into a comma separated string so I can pass it to a SQL query in the next activity - something like:
"'1312312','3524355'"
I am unable to find an expression function to iterate over the array elements, nor convert an array to a string. The only pipeline expression functions I see are to convert string to array and not the other way around.
Am I missing something basic? How can this be achieved?
Use 'join' function present in 'collection' functions in 'Add dynamic content'. For example:
join(variables('ARRAY_VARIABLE'), ',')
I had this same issue and was not totally satisfied just using the join function because it keeps the keys from the json object. Also, using an iterator approach can work but is needlessly expensive and slow if you have a long list. Here was my approach, using join and replace:
replace(replace(join(variables('VAR1'), ','), '{"Code1":', ''), '}', ''))
This will give you exactly the output you are looking for.
I got it working using a ForEach loop activity to iterate over my array and use a Set Variable task with a concat expression function to create my comma separated string.
Wish they had an iterator function in the expression language itself, that would have made it much easier.
In case, you just have two elements in the array, then you can do something like:
#concat(variables('variable_name')[0].Code1, ',', variables('variable_name')[1].Code1)