Adding unique key before duplicate JSON keys - python

I have the following JSON string:
[
{
"id":"1",
"comment":"hello"
},
{
"id":"2",
"comment":"hi"
}
]
I'm trying to make it like this:
[
{
"finding-1":{
"id":"1",
"comment":"hello"
}
},
{
"finding-2":{
"id":"2",
"comment":"hi"
}
}
]
What is the cleanest way to do this in Python?

j = [
{
"id":"1",
"comment":"hello"
},
{
"id":"2",
"comment":"hi"
}
]
n = [{f'finding-{d["id"]}': d} for d in j]
# [{'finding-1': {'comment': 'hello', 'id': '1'}}, {'finding-2': {'comment': 'hi', 'id': '2'}}]

Related

search for data from a list in MongoDB

my database looks like:
{'_id': ObjectId('3f05e2aa794e17504a6674a7'),
'lt': [
{'_id': ObjectId('6f05e2aa794e177b456674a9'), 'name': 'text1'},
{'_id': ObjectId('2f05e2aa794e1765286674a8'),'name': 'text3', }
]
}
{'_id': ObjectId('3f05e3aa791e17f23b6674aa'),
'lt': [
{'_id': ObjectId('7f05e2aa494e17f5b36674ac'), 'name': 'text12'},
{'_id': ObjectId('5f05e2aa794e1707006674ab'), 'name': 'text2'}
]
}
also i have a list
lists =["6f05e2aa794e177b456674a9", "2f05e2aa794e1765286674a8", "5f05e2aa794e1707006674ab"]
I need to find only those objects and their names that are in the list
`{
'lt': [
{'_id': ObjectId('6f05e2aa794e177b456674a9'), 'name': 'text1'},
{'_id': ObjectId('2f05e2aa794e1765286674a8'),'name': 'text3', }
]
}
{
'lt': [
{'_id': ObjectId('5f05e2aa794e1707006674ab'), 'name': 'text2'}
]
}
I wrote a query that worked for one value
id = "6f05e2aa794e177b456674a9"
objInstance = ObjectId(id)
fx = mycol.find(
{ "lt": { "$elemMatch": { "_id": objInstance }}},
{ "lt": { "$elemMatch": { "_id": objInstance }},
"_id":0, "lt._id":1 , "lt.name":1}
).limit(5)
maydata=[]
for x in fx:
print(x)
but when I rewrote it for list search
lists =["6f05e2aa794e177b456674a9", "2f05e2aa794e1765286674a8", "5f05e2aa794e1707006674ab"]
obj_ids = list(map(lambda x: ObjectId(x), lists))
fx = mycol.find(
{ "lt": { "$elemMatch": { "_id": {"$in" : obj_ids }}}},
{ "lt": { "$elemMatch": { "_id": {"$in": obj_ids }}},
"_id":0, "lt._id":1 , "lt.name":1}
).limit(5)
maydata=[]
for x in fx:
print(x)
it returned me only one element of the document
{
'lt': [
{'_id': ObjectId('2f05e2aa794e1765286674a8'),'name': 'text3', }
]
}
{
'lt': [
{'_id': ObjectId('5f05e2aa794e1707006674ab'), 'name': 'text2'}
]
}
You can use an aggregate pipeline using $filter like this:
This query project the result using $project showing only the values into lt that are also into your array.
db.collection.aggregate([
{
"$project": {
"_id": 0,
"lt": {
"$filter": {
"input": "$lt",
"cond": {
"$in": [
"$$this._id",
obj_ids
]
}
}
}
}
}
])
Example here

Construct a dictionary by using another dictionary keys and values

I have dictionary below.
my_d = {'country': ['Germany',"France"],
'games': ['Football,Motorsport'],
'bayern': ['Muller']}
I need to create a dictionary using above key and values
Each key will be added keyword in the output country.keyword
{
"query": {
"bool": {
"must": [
{
"terms": {
"country.keyword": [
"Germany",
"France"
]
}
},
{
"terms": {
"games.keyword": [
"Football",
"Motorsport"
]
}
},
{
"match": {
"bayern.keyword": ["Muller"]
}
}
]
}
}
}
if my_d = {'country': ['Germany',"France"]} or my_d = {'country': ['Germany',"France"],
'games': None,
'bayern':None}
{
"query": {
"bool": {
"must": [
{
"terms": {
"country.keyword": [
"Germany",
"France"
]
}
}
]
}
}
}
Generally I would recommend using Elasticsearch 3rd party python package do query Elasticsearch, but I believe this code should work (python 3.5+):
must_clauses = [{f"{key}.keyword": value} for key, value in my_d.items()]
terms = [{"terms": must_clause} for must_clause in must_clauses]
query_template = {
"query": {
"bool": {
"must":
terms
}
}
}

convert the python list to the dictionary with the same key but with different values

i want to convert the list data into the json format
list data:
list1 = ['coder', 'cats-dogs', 'ic', 'qwerty', 'ash', 'aish', 'ss', 'ssl', 'messi']
i expect the output:
[
{
"projectname":"coder"
},
{
"projectname":"cats-dogs"
},
{
"projectname":"ic"
},
{
"projectname":"qwerty"
},
{
"projectname":"ash"
},
{
"projectname":"aish"
},
{
"projectname":"ss"
},
{
"projectname":"sl"
},
{
"projectname":"messi"
}
]
The output that you seem to want can be obtained with :
[{"projectname" : key} for key in list1]

Remove duplicate values from list of nested dictionaries

I have list of dictionaries with nested structure. I need to remove all duplicate values. I'm newbie in Python and can't solve this task. Anyone can help me?
My list looks like:
[
{
"task_id":123,
"results":[
{
"url":"site.com",
"date":"04.18.2019"
},
{
"url":"another_site.com",
"date":"04.18.2019"
},
{
"url":"site1.com",
"date":"04.18.2019"
}
]
},
{
"task_id":456,
"results":[
{
"url":"site3.com",
"date":"04.18.2019"
},
{
"url":"site.com",
"date":"04.18.2019"
}
]
},
{
"task_id":789,
"results":[
{
"url":"site7.com",
"date":"04.18.2019"
},
{
"url":"site9.com",
"date":"04.18.2019"
},
{
"url":"site.com",
"date":"04.18.2019"
}
]
}
]
I need to set site.com only once. If any value of url is duplicated - exclude it from dict.
As result:
task 123 with 3 dicts in results
task 456 with 1 dict in results (exclude site.com)
task 789 with 2 dict in results (exclude site.com)
Desired output should looks like:
[
{
"task_id":123,
"results":[
{
"url":"site.com",
"date":"04.18.2019"
},
{
"url":"another_site.com",
"date":"04.18.2019"
},
{
"url":"site1.com",
"date":"04.18.2019"
}
]
},
{
"task_id":456,
"results":[
{
"url":"site3.com",
"date":"04.18.2019"
}
]
},
{
"task_id":789,
"results":[
{
"url":"site7.com",
"date":"04.18.2019"
},
{
"url":"site9.com",
"date":"04.18.2019"
}
]
}
]
let results to be your array.
u = set()
final = []
for dict in results:
for res in dict["results"]:
if res["url"] not in u:
u.add(res["url"])
final.append(res)
print(final)
You can use a list comprehension:
d = [{'task_id': 123, 'results': [{'url': 'site.com', 'date': '04.18.2019'}, {'url': 'another_site.com', 'date': '04.18.2019'}, {'url': 'site1.com', 'date': '04.18.2019'}]}, {'task_id': 456, 'results': [{'url': 'site3.com', 'date': '04.18.2019'}, {'url': 'site.com', 'date': '04.18.2019'}]}, {'task_id': 789, 'results': [{'url': 'site7.com', 'date': '04.18.2019'}, {'url': 'site9.com', 'date': '04.18.2019'}, {'url': 'site.com', 'date': '04.18.2019'}]}]
new_d = [{**a, 'results':[c for c in a['results'] if all(c not in b['results'] for b in d[:i])]} for i, a in enumerate(d)]
Output:
[
{
"task_id": 123,
"results": [
{
"url": "site.com",
"date": "04.18.2019"
},
{
"url": "another_site.com",
"date": "04.18.2019"
},
{
"url": "site1.com",
"date": "04.18.2019"
}
]
},
{
"task_id": 456,
"results": [
{
"url": "site3.com",
"date": "04.18.2019"
}
]
},
{
"task_id": 789,
"results": [
{
"url": "site7.com",
"date": "04.18.2019"
},
{
"url": "site9.com",
"date": "04.18.2019"
}
]
}
]
people = {
1: {'name': 'John',},
2: {'name': 'Marie'},
3: {'name': 'Ann',},
4: {'name': 'John'},
}
print(people)
unique = {}
for key, value in people.items():
if value not in unique.values():
unique[key] = value
print(unique)
try these

ordering json in python mapping object

I am using elasticsearch where the query is to be posted in json and should be in standard order or else the result will be wrong. the problem is that the python is changing my json ordering. my original json query is.
x= {
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*a*"
}
},
"filter": {
"and": {
"filters": [
{
"term": {
"city": "london"
}
},
{
"term": {
"industry.industry_not_analyed": "oil"
}
}
]
}
}
}
},
"facets": {
"industry": {
"terms": {
"field": "industry.industry_not_analyed"
}
},
"city": {
"terms": {
"field": "city.city_not_analyzed"
}
}
}
}
but the resulting python object is as follow.
{
'query': {
'filtered': {
'filter': {
'and': {
'filters': [
{
'term': {
'city': 'london'
}
},
{
'term': {
'industry.industry_not_analyed': 'oil'
}
}
]
}
},
'query': {
'query_string': {
'query': '*a*'
}
}
}
},
'facets': {
'city': {
'terms': {
'field': 'city.city_not_analyzed'
}
},
'industry': {
'terms': {
'field': 'industry.industry_not_analyed'
}
}
}
}
the result is different than what I need how do I solve this.
Use OrderedDict() instead of {}. Note that you can't simply use OrderedDict(query=...) because that would create an unordered dict in the background. Use this code instead:
x = OrderedDict()
x['query'] = OrderedDict()
...
I suggest to implement a builder for this:
x = Query().filtered().query_string("*a*").and()....

Categories