some_json
{
"id": 1,
"foo": "foo",
"bar": "bar",
"baz": "baz"
}
assume all the keys in some_json are columns
then normally when i update the database,
i'll do something like
item = db.query.filter_by(id=some_json['id']).first()
if item:
item.foo = some_json['foo']
item.bar = some_json['bar']
item.baz = some_json['baz']
db.session.add(item)
db.commit()
however, this can obviously be tedious if have more and more keys
is there an easier way to update the table rows just by simply passing the json?
By converting json to dectionery, which maps the table model table_model in your case, you can write your code as follows:
a= {
"id": 1,
"foo": "foo",
"bar": "bar",
"baz": "baz"
}
item = table_model.query.filter_by(id=some_json['id']).first()
if item:
item=table_model(**a)
db.session.add(item)
db.commit()
Related
i'm new to Couchbase and N1QL syntax and i'm facing an issue.
Let's say we have 3 type of documents:
Doc1 of TypeA with key = typeA:Doc1
{
"type": "typeA"
"id": "Doc1",
"sequences": [
"typeB:Doc2"
]
}
Doc2 of TypeB with key = typeB:Doc2
{
"id": "Doc2",
"processors": [
{
"order": 1,
"id": "typeC:Doc3"
}
]
}
Doc3 of TypeC with key = typeC:Doc3
{
"id": "Doc3",
"prop": "value"
}
What i want to achieve is to nest these 3 objects by their document keys in ordere to have a unique document with this structure:
{
"id": "Doc1",
"sequences": [
{
"id": "Doc2",
"processors": [
{
"order": 1,
"id": "Doc3",
"prop": "value"
}
]
}
]
What i've done is to nest the first two documents to obtain a partial result. But i'm tryng to integrate also the third document.
Here's my attempt:
SELECT dev.*,
ARRAY sq_i FOR sq_i IN prseq END AS sequences
FROM data dev
NEST data prseq ON KEYS dev.sequences
WHERE dev.type = 'TypeA'
Can anyone help me with the third level of nesting?
Thank you.
Use subqueries
SELECT dt.*,
(SELECT ds.*,
(ARRAY OBJECT_ADD((SELECT RAW dp FROM data AS dp USE KEYS v.id)[0], "order", v.`order`)
FOR v IN ds.processors
END) AS processors
FROM data AS ds USE KEYS dt.sequences) AS sequences
FROM data AS dt
WHERE dt.type = 'TypeA';
I am trying to build a JSON from the following table
name | flag
------+------
foo | fail
bar | pass
using the query,
DECLARE #JSONDATA nvarchar(MAX) = (SELECT [name], [flag]
FROM test
FOR JSON AUTO, ROOT('students'))
SET #JSONDATA = JSON_MODIFY(#JSONDATA, '$.class','10')
The generated JSON here is
{
"students": [
{
"name": "foo",
"flag": "fail"
},
{
"name": "bar",
"flag": "pass"
}
],
"class": "10"
}
I need to the class element at the very first node of the JSON. Is there any way, using JSON_MODIFY ?
Fiddle
At a loss forcing a sequence via modify.
Perhaps an alternative
Select class=10
,students = (SELECT [name], [flag] FROM test FOR JSON AUTO)
For JSON path, without_array_wrapper
Returns
{
"class": 10,
"students": [{
"name": "foo",
"flag": "fail"
}, {
"name": "bar",
"flag": "pass"
}]
}
EDIT- Updated SELECT as suggested by GSerg
I have some data that looks like:
{
"hello": {
"foo": "x",
"y": "z"
},
"foo": "a",
"bar": {
{
"foo": "b"
}
}
}
How can I get all values with key foo, wherever they are?
You can search using recursive decent
Solution
jq '..|.foo?'
Demo
https://jqplay.org/s/Xp64LfJFBc
Documentation
https://stedolan.github.io/jq/manual/#RecursiveDescent:..
Suppose I have json like this:
[
{
"a": 1,
"b": 2,
"c": 3,
},
{
"a": 1,
"b": 2,
"d": 4
}
]
Is it possible to write a JsonPath which will give me:
{
"a": 1,
"b": 2,
"c": 3,
"d": 4
}
The order of the elements in the list is not sure, however the element names I know beforehand. So I thought I could use JsonPath to select all elements with names 'a', 'b', 'c' or 'd' and put them in a single object. However, I now have tried several things and I start to doubt whether what I want is possible.
For example I can select an object which has an element c with:
$[?(#.c)]
And I can also select just the value of d with something like:
$..d
But I can not find any way to comebine them into a single object. Is what I want possible with JsonPath or is this a limitation of JsonPath?
I think the appropriate way to do this is to remove the second object and apply the difference between them to the first one:
[
{ "op": "remove", "path": "/1" },
{ "op": "add", "path": "/0/d", "value": 4 }
]
Assuming you're OK with polyfilling for IE (it's ES6), use Object.assign:
const object1 = {
a: 1,
b: 2,
c: 3
};
const object2 = {
d: 4
}
const objectMerged = Object.assign({}, object1, object2);
console.log(objectMerged.c);
console.log(objectMerged.d);
For full details, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Browser_compatibility
In my Django project, my View is converting a ValuesQuerySet to a JSON string:
import json
# ...
device_list = list(Device.objects.values())
device_json = json.dumps(device_list)
The resulting JSON string:
[{"field1": "value", "location_id": 1, "id": 1, "field2": "value"},
{...}]
How can I include the data within the location object represented by "location_id": 1, instead of the ID number? Something like this:
[{"field1": "value", "location_name": "name", "location_region": "region", "another_location_field": "value", "id": 1, "field2": "value"},
{...}]
I found that you can use Field Lookups to follow relationships and access fields in another related model:
import json
# ...
device_list = list(Device.objects.values('field1', 'field2', 'location__name', 'location__region'))
json.dumps(device_list)
The resulting JSON string:
[{field1": "value", "field2": "value", "location__name": "name", "location__region": "region"},
{...}]