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])
Related
I have a column named data and I have to update its content from something like {} to [{}] for each record in table A, I tried to use JSON_ARRAY() but it gives me a quoted
["{\"something\": \"true\"}"]
but I'd like to have something like
[{ "something": "true" }]
How I do it now?
SELECT JSON_ARRAY(data) FROM A;
How should I update it either using JSON_SET() or UPDATE?
You need to use a path to get the data as JSON, rather than referring to the column by itself. The path $ means the top-level object.
update A
SET data = CASE
WHEN data IS NULL THEN '[]' -- NULL becomes empty array
WHEN LEFT(data, 1) = '[' THEN data -- leave existing array alone
ELSE JSON_ARRAY(data->"$") -- put object inside array
END
DEMO
Try using
SELECT JSON_ARRAY_AGG(JSON_OBJECT(data)) from A;
I want to retrieve data from an external nested JSON file on my seed.rb
The JSON looks like this:
{"people":[{"name":"John", "age":"23"}, {"name":"Jack", "age":"25"}]}
I saw a solution on GitHub but it only works on non-nested JSON.
Let's say you have JSON file db/seeds.json:
{"people":[{"name":"John", "age":"23"}, {"name":"Jack", "age":"25"}]}
You can use it like this in your db/seeds.rb:
seeds = JSON.parse(Rails.root.join("db", "seeds.json"), symbolize_names: true)
User.create(seeds[:people])
seeds[:people] in this case is array of hashes with user attributes
if you have:
json_data = {"people":[{"name":"John", "age":"23"}, {"name":"Jack", "age":"25"}]}
when you do:
json_data[:people]
you'll get an array:
[{:name=>"John", :age=>"23"}, {:name=>"Jack", :age=>"25"}]
if you want to use this array to populate a model, you can do:
People.create(json_data[:people])
if you want to read each item values, you can iterate through your data, like:
json_data[:people].each {|p| puts p[:name], p[:age]}
I have a JSON object in this format data = {"1": {"col1":"a", "col2":"b"}, "2": {"col1":"c", "col2":"d"}....,"99":{"col1":"asd", "col2":"exm"}}. I would like to get all the values for col_1 and col_2 using athena. How do I achieve this using athena?
I was able to solve it by converting it to Map and then unnesting it like
select key, value from table where
unnest(cast(json_parse(data) as Map(varchar,JSON)) as t(key,value))
Ignore that it is a JSON format (since it is not simple enough) and use the regex function:
SELECT regexp_extract_all(column_data, '"col1":"([a-z]+)",'); -- [a, c, ... asd]
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
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.