This is my first attempt at couchbase. My json doc looks like this:
{
"member_id": "12345",
"devices": [
{
"device_id": "1",
"hashes": [
"h1",
"h2",
"h3",
"h4"
]
},
{
"device_id": "2",
"hashes": [
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"h7"
]
}
]
}
I want to create a view which tells me all member_ids for a given hash.
Something like this:
h1["12345","233","2323"] //233,2323 are other member id
h2["12345"]
The member_id should appear once in the set.
I wrote a map function
function (doc, meta) {
for(i=0;i< doc.devices.length;i++)
{
for(j=0;j< doc.devices[i].hashes.length;j++) {
emit(doc.devices[i].hashes[j],null)
}
}
}
and this returns
h1 "12345"
h1 "12345"
h2 "12345"
h1 "233"
but I'm not able to move forward from here. How should I change my map function to reduce the result?
Map function. Mostly yours, but emits meta.id as a value.
function(doc, meta) {
for(i=0; i< doc.devices.length; i++) {
for(j=0; j< doc.devices[i].hashes.length; j++) {
emit(doc.devices[i].hashes[j], meta.id)
}
}
}
Reduce function. Is just return unique array from values (taken from https://stackoverflow.com/a/13486540/98509)
function(keys, values, rereduce) {
return values.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});
}
Related
my resolver in schema.py looks like this
def resolve_areas(self, info, **kwargs):
result = []
dupfree = []
user = info.context.user
areas = BoxModel.objects.filter(client=user, active=True).values_list('area_string', flat=True)
In GraphiQL I am using this query:
{
areas {
edges {
node {
id
name
}
}
}
}
And get Output that starts like this:
{
"data": {
"areas": {
"edges": [
{
"node": {
"id": "QXJlYTpkZWZ",
"name": "default"
}
},
{
"node": {
"id": "QXJlYTptZXN",
"name": "messe"
}
},
{
"node": {
"id": "QXJlYTptZXN",
"name": "messe"
}
},
But i want distinct values on the name variable
(Using a MySQL Database so distinct does not work)
SOLVED:
distinct was not working. so i just wrote a short loop which tracked onlye the string names duplicates in a list and only appended the whole "area" object if it's name has not been added to the duplicates list yet
result = []
dupl_counter = []
for area in areas:
if area not in dupl_counter:
dupl_counter.append(area)
result.append(Area(name=area))
print(area)
Im trying to read json file with qt c++. After reading i will do some operations. I've read my json like it, but i want to print spesific datas, which include "float".
After finding them, i will assign a value each of these.
Thanks for any help
{
"A":[
{
"GPS":[
{
"GPS ID":[
"integer",
"0"
],
"GPS Mod":[
"integer",
"1"
],
"GPS Utc":[
"float",
"2"
],
"GPS Latitude":[
"float",
"3"
],
"GPS Longitude":[
"float",
"4"
]
}
]
Here is what I have tried up to now:
QByteArray data = file.readAll();
QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonObject root = doc.object();
QJsonArray tlmtArray = root.value("A").toArray();
for(int i=0; i<tlmtArray.size(); i++)
{
QJsonObject obj = tlmtArray.at(i).toObject();
}
Actually, you seem to already know what you need to do to handle Json data, you just have to continue to unpack your nested structures until the leaf data.
I'm not sure what was your issue, you just had to continue what you were doing but anyway, I have written an example that prints in the console the inner GPS data of the following structure (the one that you provided, I just added the missing braces and brackets):
{
"A":
[
{
"GPS":
[
{
"GPS ID":[
"integer",
"0"
],
"GPS Mod":[
"integer",
"1"
],
"GPS Utc":[
"float",
"2"
],
"GPS Latitude":[
"float",
"3"
],
"GPS Longitude":[
"float",
"4"
]
}
]
}
]
}
Here is the example function:
int printJsonData(const QByteArray & json_data)
{
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(json_data, &err);
if(err.error != QJsonParseError::NoError)
return -1; // Failure
QJsonObject root = doc.object();
QJsonArray tlmtArray = root.value("A").toArray();
for(int i = 0; i < tlmtArray.size(); ++i)
{
QJsonObject obj = tlmtArray[i].toObject();
QJsonArray gps_array = obj.value("GPS").toArray();
for(int j = 0; j < gps_array.size(); ++j)
{
QJsonObject gps_obj = gps_array[j].toObject();
for(QJsonObject::const_iterator cit = gps_obj.constBegin(); cit != gps_obj.constEnd(); ++cit)
{
std::cout << cit.key().toStdString() << ": (";
QJsonArray inner_data = cit.value().toArray();
for(int k = 0; k < inner_data.size(); ++k)
{
std::cout << inner_data[k].toString().toStdString() << (k < inner_data.size()-1 ? "," : "");
}
std::cout << ")\n";
}
}
}
return 0;
}
Output:
GPS ID: (integer,0)
GPS Latitude: (float,3)
GPS Longitude: (float,4)
GPS Mod: (integer,1)
GPS Utc: (float,2)
Please note that you really should use the methods QJsonObject::contains(), QJsonValue::isArray(), QJsonValue::isObject() and QJsonValue::isString() that Qt provides in order to check that the data you are extracting are exactly what you expect them to be. I didn't write such checks in the example because it is just an example and I wanted to avoid clutter and make the code less readable.
how can I find duplicate values on a list,
Let's say I got a List like this:
List<Map<String, dynamic>> users = [
{ "name": 'John', 'age': 18 },
{ "name": 'Jane', 'age': 21 },
{ "name": 'Mary', 'age': 23 },
{ "name": 'Mary', 'age': 27 },
];
How I can iterate the list to know if there are users with the same name?
A simple way would be this:
void main() {
List<Map<String, dynamic>> users = [
{ "name": 'John', 'age': 18 },
{ "name": 'Jane', 'age': 21 },
{ "name": 'Mary', 'age': 23 },
{ "name": 'Mary', 'age': 27 },
];
List names = []; // List();
users.forEach((u){
if (names.contains(u["name"])) print("duplicate ${u["name"]}");
else names.add(u["name"]);
});
}
Result:
duplicate Mary
Probably a cleaner solution with extensions.
By declaring:
extension ListExtensions<E> on List<E> {
List<E> removeAll(Iterable<E> allToRemove) {
if (allToRemove == null) {
return this;
} else {
allToRemove.forEach((element) {
this.remove(element);
});
return this;
}
}
List<E> getDupes() {
List<E> dupes = List.from(this);
dupes.removeAll(this.toSet().toList());
return dupes;
}
}
then you can find your duplicates by calling List.getDupes()
Note that the function removeAll doesn't exist in my current Dart library, in case you're reading this when they implement it somehow.
Also keep in mind the equals() function. In a List<String>, ["Rafa", "rafa"] doesn't contain duplicates.
If you indeed want to achieve this level of refinement, you'd have to apply a distinctBy function:
extension ListExtensions<E> on List<E> {
List<E> removeAll(Iterable<E> allToRemove) {
if (allToRemove == null) {
return this;
} else {
allToRemove.forEach((element) {
this.remove(element);
});
return this;
}
}
List<E> distinctBy(predicate(E selector)) {
HashSet set = HashSet();
List<E> list = [];
toList().forEach((e) {
dynamic key = predicate(e);
if (set.add(key)) {
list.add(e);
}
});
return list;
}
List<E> getDupes({E Function(E) distinctBy}) {
List<E> dupes = List.from(this);
if (distinctBy == null) {
dupes.removeAll(this.toSet().toList());
} else {
dupes.removeAll(this.distinctBy(distinctBy).toSet().toList());
}
return dupes;
}
}
I had a feeling Rafael's answer had code similar to Kotlin so I dug around and saw that these functions are part of the kt_dart library which basically gets the Kotlin standard library and ports it to Dart.
I come from a Kotlin background so I use this package often. If you use it, you can simply make the extension this much shorter:
extension KtListExtensions<T> on KtList<T> {
KtList<T> get duplicates => toMutableList()..removeAll(toSet().toList());
}
just make sure to add kt_dart on your pubspec: kt_dart: ^0.8.0
Example
final list = ['apples', 'oranges', 'bananas', 'apples'].toImmutableList();
final duplicates = list.duplicates; // should be ['apples'] in the form of an ImmutableList<String>
void main() {
List<String> country = [
"Nepal",
"Nepal",
"USA",
"Canada",
"Canada",
"China",
"Russia",
];
List DupCountry = [];
country.forEach((dup){
if(DupCountry.contains(dup)){
print("Duplicate in List= ${dup}");
}
else{
DupCountry.add(dup);
}
});
}
I wrote the following parser (paste into http://pegjs.org/online and it works):
Expression = Pairs / FullTextWithPairs
Pairs = (';'? _ p:Pair { return p; })*
FullTextWithPairs = fto:FullText po:Pairs
{
var arr = [];
if (fto) {
arr.push(fto);
}
return arr.concat(po);
}
FullText = ft:ValueString ';'
{
return {'key' : 'fullText', 'op': '=', 'value': ft};
}
Pair = k:Field op:Operator v:ValueString
{
var res = {'key' : k, 'op': op, 'value': v};
console.log(res);
return res;
}
Operator = ('<>' / [=><])
ValueString = vs:[^;]+ {return vs.join('');}
Field = 'location' / 'application' / 'count'
_ = ' '*
It parses this string of key-value pairs: location=USA; application<>app; count>5
to this:
[
{
"key": "location",
"op": "=",
"value": "USA"
},
{
"key": "application",
"op": "<>",
"value": "app"
},
{
"key": "count",
"op": ">",
"value": "5"
}
]
The problem is I want to enable a free-text search as well, which is entered before the key-value pairs, for example:
this: free text foobar; location=USA; application<>app; count>5
and get this:
[
{
"key": "fullText",
"op": "=",
"value": "free text foobar"
},
{
"key": "location",
"op": "=",
"value": "USA"
},
{
"key": "application",
"op": "<>",
"value": "app"
},
{
"key": "count",
"op": ">",
"value": "5"
}
]
The parser should recognize that the first part is not a key-value pair (according to "Pair" rule) and insert it as "fullText" object.
Basically "Expression" rule should do it, according to what I read in the docs - A / B means if A doesn't pass the B is tried. In the second case "Paris" is faild because "free text foobar" doesn't pass the Pairs rule, but it just throws an exception instead of moving on.
Congrats to whomever survived up to here, what am I doing wrong? :)
Thank you
Played with the grammar some more, the solution was to use !Pair and (for some reason) to change the order of the "Expression" rule:
Expression = FullTextWithPairs / Pairs
Pairs = (';'? _ p:Pair { return p; })*
FullTextWithPairs = fto:FullText po:Pairs
{
var arr = [];
if (fto) {
arr.push(fto);
}
return arr.concat(po);
}
FullText = !Pair ft:ValueString ';'
{
return {'key' : 'fullText', 'op': '=', 'value': ft};
}
Pair = _? k:Field op:Operator v:ValueString
{
var res = {'key' : k, 'op': op, 'value': v};
console.log(res);
return res;
}
Operator = ('<>' / [=><])
ValueString = vs:[^;]+ {return vs.join('');}
Field = 'location' / 'application' / 'count'
_ = ' '*
I have 3 variantMaps created from JSON and I would like to substitute
for example all 3 things from 3d with 2nd and than second to first to the properties.
QVariantMap wholeMapToChange; //1.
QVariantMap propertiesMapToChange; //2.
QVariantMap cmdMap; //3.
1st contain this JSON data but in map:
{
properties {
"A": true,
"B": true,
"fieldName": "ewfqfqewf",
"C": false,
"fieldPassword": "451541611",
"isBtnSignOnClicked": true
},
type: "xyz"
}
2nd contain this JSON data but in map:
{
"A": true,
"B": true,
"fieldName": "ewfqfqewf",
"C": false,
"fieldPassword": "451541611",
"isBtnSignOnClicked": true
}
3d contain this JSON data but in map:
{
"fieldName": "nick",
"fieldPassword": "0000",
"isBtnSignOnClicked": true
}
What I see as a possibility for substituing 3 with 2 is to create cycle
for (QVariantMap::const_iterator it = propertiesMapToChange.begin(); it != propertiesMapToChange.end(); ++it){
for (QVariantMap::const_iterator itt = cmdMap.begin(); itt != cmdMap.end(); ++itt){
///here would be the comparig...
}
}
But I dont think this is good solution... I would like to ask you for advice or opinion, whether its correct, or there exist better way to do that.
Thx
It is the right solution if the maps aren't too big, since the cost is N*M. But it is a premature pessimization. You should implement the loop to have N+M cost: after all, the maps are sorted, so you only need to iterate each map once.
A complete example:
// https://github.com/KubaO/stackoverflown/tree/master/questions/json-map-iter-39979440
#include <QtCore>
QVariantMap replaceMap(QVariantMap dst, const QVariantMap & src) {
auto dit = dst.begin();
auto sit = src.begin();
while (dit != dst.end() && sit != src.end()) {
if (sit.key() < dit.key()) {
++ sit;
continue;
}
if (dit.key() < sit.key()) {
++ dit;
continue;
}
Q_ASSERT(sit.key() == dit.key());
dit.value() = sit.value();
++ sit;
++ dit;
}
return dst;
}
int main() {
auto json1 = QJsonDocument::fromJson({R"ZZ({
"properties":{
"A":true,
"B":true,
"fieldName":"ewfqfqewf",
"C":false,
"fieldPassword":"451541611",
"isBtnSignOnClicked":true
},
"type":"xyz"
})ZZ"}).toVariant().value<QVariantMap>();
auto json2 = QJsonDocument::fromJson({R"ZZ({
"A":true,
"B":true,
"fieldName":"ewfqfqewf",
"C":false,
"fieldPassword":"451541611",
"isBtnSignOnClicked":true
})ZZ"}).toVariant().value<QVariantMap>();
auto json3 = QJsonDocument::fromJson(
{R"ZZ({
"fieldName":"nick",
"fieldPassword":"0000",
"isBtnSignOnClicked":true
})ZZ"}).toVariant().value<QVariantMap>();
json2 = replaceMap(json2, json3);
auto props = replaceMap(json1["properties"].value<QVariantMap>(), json2);
json1["properties"] = props;
qDebug() << QJsonDocument::fromVariant(json1).toJson().constData();
}
Output:
{
"properties": {
"A": true,
"B": true,
"C": false,
"fieldName": "nick",
"fieldPassword": "0000",
"isBtnSignOnClicked": true
},
"type": "xyz"
}