I'm working with Postgres 9.5 json arrays. My json object is an array of objects:
[{'name' : ... 'field2':...}, {'name' : ...}, {'name', ...}]
I'm trying to get all the 'name' values from the array. I can get a specific one with (for example for the first array entry):
select jsonarr->0->'name' from items;
But I can't get them all and I didn't find a way looking at the docs (http://www.postgresql.org/docs/9.5/static/functions-json.html). I'm searching for something like:
select jsonarr->*->'name' from items;
how can I do it?
thanks, dan
Related
Many similar questions, but unfortunately non helped me solve my problem. I tried to && and #> and similar, but no success.
I have a postgres DB with a table, that has a "value" column typed "json". All rows have the same basic structure, a simple JSON object, with the att "value" holding an array of strings:
{
value: ['one', 'two', 'three']
}
I need to make a query accepting an array of strings and returns all the rows, in which the value array and the passed array of strings have at least one common element.
Following the upper example, if I send ['one', 'four'], it should return the row with value: ['one', 'two', 'three'], since there is an intersection - 'one'.
If I send the array ['four', 'five', 'six'], it will not return this row.
You can use the ?| operator for that. But as you are using json and not the recommended jsonb type, you need to cast your column:
select *
from the_table
where (value::jsonb -> 'value') ?| array['one', 'four']
I want to iterate over json array that looks likes this.
[
{"key":"value"},
{"key2":"value2"},
{"key3":"value3"},
]
I have tried with json library but it's not possible to iterate over it. The index is not aƱways 0 but succesive
json_result = json.loads(json_var)
print(json_result[0])
print(json_result[0]["key"])
print(json_result[1])
print(json_result[1]["key1"])
comes with:
{"key":"value"}
value
{"key1":"value1"}
value1
So, I would like to get values without accessing their names. something like this:
for x in json_result:
print(json_result[0][x])
Try this:
for i in list_json_var:
for key in i:
print(i[key])
The default output of JsonConvert.SerializeObject(ds2, Formatting.Indented) is an array of objects. I'd like to get an array of arrays with just the row data and not reapeating of the column name in each row.
I'd like to be able to present my recordset to DataTables as an array of columns and an array of row data items. Getting the column names into a variable is pretty easy and fast, but I'm wondering what's the fastest way to get the row data into an array.
I'm not interested in compressing the data in transit as described in another answer. The data needs to be processed on the client side in a jQuery DataTable. The more rows returned the slower the JavaScript gets.
To be clear, I want the JSON Serializer to return data in the following format:
aoColumns: [
"Name",
"Age",
"Location"
],
aaData: [
["Bob", 32, "Group1"],
["Sara", 43, "Group1"],
["Gary", 41, "Group2"]
]
aaData could be any name as DataTables can be told where to find the data and the column names.
I'm using Postgrex in Elixir, and when it returns query results, it returns them in the following struct format:
%{columns: ["id", "email", "name"], command: :select, num_rows: 2, rows: [{1, "me#me.com", "Bobbly Long"}, {6, "email#tts.me", "Woll Smoth"}]}
It should be noted I am using Postgrex directly WITHOUT Ecto.
The columns (table headers) are returned as a collection, but the results (rows) are returned as a list of tuples. (which seems odd, as they could get very large).
I'm trying to find the best way to programmatically create JSON objects for each result in which the JSON key is the column title and the JSON value the corresponding value from the tuple.
I've tried creating maps from both, merging and then serialising to JSON objects but it seems there should be an easier/better way of doing this.
Has anyone dealt with this before? What is the best way of creating a JSON object from a separate collection and tuple?
Something like this should work:
result = Postgrex.query!(...)
Enum.map(result.rows, fn row ->
Enum.zip(result.columns, Tuple.to_list(row))
|> Enum.into(%{})
|> JSON.encode
end)
This will result in a list of json objects where each row in the resultset is a json object.
I have this JSON array, but I don't know how display it.
{
"grupy": [{
"id_grupa_parametrow": "1",
"id_grupa_nadrzedna": "0",
"nazwa_grupy": "1_1",
"opis_grupy": "hdghgh",
"kolejnosc": "1233"
}]
}
I tried:
result["grupy"].id_grupa_parametrow;
but it doesn't work.
You can use some online json editors to find your data easily. Anyway, you have an array defined for groupy index. So, use numerical indexes first:
result["grupy"][0].id_grupa_parametrow;
What you have to know is that {} means that it's an object and [] is an array. So result in an object which has grupy property - that's why you have to use a dot here - result.grupy.
grupy is an array to you should use [0] index - result.grupy[0].
And so on...
This is the right way in this case:
result.grupy[0].id_grupa_parametrow;
result.grupy[0].id_grupa_parametrow;