How to export JSON with py2neo? - python

I want to response the JSON which likes exported from neo4j browser.
The structure like this.
[
{
"p": {
"start": {
"identity": 2,
"labels": [
"Person"
],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
}
},
"end": {
"identity": 155,
"labels": [
"Movie"
],
"properties": {
"title": "Something's Gotta Give",
"released": 2003
}
},
"segments": [
{
"start": {
"identity": 2,
"labels": [
"Person"
],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
}
},
"relationship": {
"identity": 221,
"start": 2,
"end": 155,
"type": "ACTED_IN",
"properties": {
"roles": [
"Julian Mercer"
]
}
},
"end": {
"identity": 155,
"labels": [
"Movie"
],
"properties": {
"title": "Something's Gotta Give",
"released": 2003
}
}
}
],
"length": 1
}
},
{
"p": {
"start": {
"identity": 2,
"labels": [
"Person"
],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
}
},
"end": {
"identity": 88,
"labels": [
"Movie"
],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
}
},
"segments": [
{
"start": {
"identity": 2,
"labels": [
"Person"
],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
}
},
"relationship": {
"identity": 114,
"start": 2,
"end": 88,
"type": "ACTED_IN",
"properties": {
"roles": [
"Shane Falco"
]
}
},
"end": {
"identity": 88,
"labels": [
"Movie"
],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
}
}
}
],
"length": 1
}
}
]
In order to export json,I use the code like this:
class JsonExtactor(object):
def __init__(self):
super(JsonExtactor,self).__init__()
self.graph = Graph(
"http://localhost:7474",
auth=('neo4j','neo4j'))
def run(self):
try:
return(self.graph.run("MATCH p=()-[r:ACTED_IN]->() RETURN p LIMIT 2", {}).data())
except Exception as e:
print(e)
return None
JsonExtactor().run()
But it only returns a list like this:
[{'p': Path(Node('Person', born=1964, name='Keanu Reeves'), ACTED_IN(Node('Person', born=1964, name='Keanu Reeves'), Node('Movie', released=2003, title="Something's Gotta Give")))}, {'p': Path(Node('Person', born=1964, name='Keanu Reeves'), ACTED_IN(Node('Person', born=1964, name='Keanu Reeves'), Node('Movie', released=2000, tagline='Pain heals, Chicks dig scars... Glory lasts forever', title='The Replacements')))}]
How to response the json format which is like above json structure ?
Please help me
Thanks

Related

update nested json object in python

I have a json file name input which as follows
{
"abc": {
"dbc": {
"type": "string",
"metadata": {
"description": "Name of the namespace"
}
},
"fgh": {
"type": "string",
"metadata": {
"description": "Name of the Topic"
}
}
},
"resources": [
{
"sku": {
"name": "[parameters('sku')]"
},
"properties": {},
"resources": [
{
"resources": [
{
"resources": [
{
"properties": {
"filterType": "SqlFilter",
"sqlFilter": {
"sqlExpression": "HAI"
}
}
}
]
}
]
}
]
}
]
}
I want "sqlExpression": "HAI" value to be replaced with BYE as below
"sqlExpression": "BYE"
I want python code to do it, I tried the below code but not working
input['resources'][0]['resources'][0]['resources'][0]['resources'][0][properties][0][sqlFilter][0][sqlExpression][0]='BYE'
inp = {
"abc": {
"dbc": {
"type": "string",
"metadata": {
"description": "Name of the namespace"
}
},
"fgh": {
"type": "string",
"metadata": {
"description": "Name of the Topic"
}
}
},
"resources": [
{
"sku": {
"name": "[parameters('sku')]"
},
"properties": {},
"resources": [
{
"resources": [
{
"resources": [
{
"properties": {
"filterType": "SqlFilter",
"sqlFilter": {
"sqlExpression": "HAI"
}
}
}
]
}
]
}
]
}
]
}
inp['resources'][0]['resources'][0]['resources'][0]['resources'][0]['properties']['sqlFilter']['sqlExpression']='BYE'
print(inp)
Result
{'abc': {'dbc': ...truncated... {'sqlExpression': 'BYE'}}}]}]}]}]}

How can I export JSON which looks like Neo4j Browser export button?

I would like to reproduce the Neo4j Browser JSON format which is something like this :
[
{
"p": {
"start": {
"identity": 2,
"labels": [
"Person"
],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
}
},
"end": {
"identity": 155,
"labels": [
"Movie"
],
"properties": {
"title": "Something's Gotta Give",
"released": 2003
}
},
"segments": [
{
"start": {
"identity": 2,
"labels": [
"Person"
],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
}
},
"relationship": {
"identity": 221,
"start": 2,
"end": 155,
"type": "ACTED_IN",
"properties": {
"roles": [
"Julian Mercer"
]
}
},
"end": {
"identity": 155,
"labels": [
"Movie"
],
"properties": {
"title": "Something's Gotta Give",
"released": 2003
}
}
}
],
"length": 1
}
},
{
"p": {
"start": {
"identity": 2,
"labels": [
"Person"
],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
}
},
"end": {
"identity": 88,
"labels": [
"Movie"
],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
}
},
"segments": [
{
"start": {
"identity": 2,
"labels": [
"Person"
],
"properties": {
"born": 1964,
"name": "Keanu Reeves"
}
},
"relationship": {
"identity": 114,
"start": 2,
"end": 88,
"type": "ACTED_IN",
"properties": {
"roles": [
"Shane Falco"
]
}
},
"end": {
"identity": 88,
"labels": [
"Movie"
],
"properties": {
"tagline": "Pain heals, Chicks dig scars... Glory lasts forever",
"title": "The Replacements",
"released": 2000
}
}
}
],
"length": 1
}
}
]
I've tried to do it with an httpie command to in order to retrieve what is sent to the transactional cypher endpoint but I got something with data, rows and metadata.
My goal is to retrieve a part of my database with a Cypher request, and to retrieve via Python, the JSON like the one of the Neo4j Browser button.
Thanks for your help !
You can retrieve via Python, and keep it as dict type, then you can use json to save this dict with parameter "indent".
import json
my_dict = {'name': ['Raphael', 'Donatello'],
'mask': ['red', 'purple'],
'weapon': ['sai', 'bo staff']}
with open('/Users/peter/temp/test.json', 'w', encoding='utf-8') as f:
json.dump(my_dict, f, indent=2)
json.dump

Is there a way to add curly brackets around a list of dictionaries already existing within a JSON file?

I currently have two JSONS that I want to merge into one singular JSON, additionally I want to add in a slight change.
Firstly, these are the two JSONS in question.
An intents JSON:
[
{
"ID": "G1",
"intent": "password_reset",
"examples": [
{
"text": "I forgot my password"
},
{
"text": "I can't log in"
},
{
"text": "I can't access the site"
},
{
"text": "My log in is failing"
},
{
"text": "I need to reset my password"
}
]
},
{
"ID": "G2",
"intent": "account_closure",
"examples": [
{
"text": "I want to close my account"
},
{
"text": "I want to terminate my account"
}
]
},
{
"ID": "G3",
"intent": "account_creation",
"examples": [
{
"text": "I want to open an account"
},
{
"text": "Create account"
}
]
},
{
"ID": "G4",
"intent": "complaint",
"examples": [
{
"text": "A member of staff was being rude"
},
{
"text": "I have a complaint"
}
]
}
]
and an entities JSON:
[
{
"ID": "K1",
"entity": "account_type",
"values": [
{
"type": "synonyms",
"value": "business",
"synonyms": [
"corporate"
]
},
{
"type": "synonyms",
"value": "personal",
"synonyms": [
"vanguard",
"student"
]
}
]
},
{
"ID": "K2",
"entity": "beverage",
"values": [
{
"type": "synonyms",
"value": "hot",
"synonyms": [
"heated",
"warm"
]
},
{
"type": "synonyms",
"value": "cold",
"synonyms": [
"ice",
"freezing"
]
}
]
}
]
The expected outcome is to create a JSON file that mimics this structure:
{
"intents": [
{
"intent": "password_reset",
"examples": [
{
"text": "I forgot my password"
},
{
"text": "I want to reset my password"
}
],
"description": "Reset a user password"
}
],
"entities": [
{
"entity": "account_type",
"values": [
{
"type": "synonyms",
"value": "business",
"synonyms": [
"company",
"corporate",
"enterprise"
]
},
{
"type": "synonyms",
"value": "personal",
"synonyms": []
}
],
"fuzzy_match": true
}
],
"metadata": {
"api_version": {
"major_version": "v2",
"minor_version": "2018-11-08"
}
},
"dialog_nodes": [
{
"type": "standard",
"title": "anything_else",
"output": {
"generic": [
{
"values": [
{
"text": "I didn't understand. You can try rephrasing."
},
{
"text": "Can you reword your statement? I'm not understanding."
},
{
"text": "I didn't get your meaning."
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"conditions": "anything_else",
"dialog_node": "Anything else",
"previous_sibling": "node_4_1655399659061",
"disambiguation_opt_out": true
},
{
"type": "event_handler",
"output": {
"generic": [
{
"title": "What type of account do you hold with us?",
"options": [
{
"label": "Personal",
"value": {
"input": {
"text": "personal"
}
}
},
{
"label": "Business",
"value": {
"input": {
"text": "business"
}
}
}
],
"response_type": "option"
}
]
},
"parent": "slot_9_1655398217028",
"event_name": "focus",
"dialog_node": "handler_6_1655398217052",
"previous_sibling": "handler_7_1655398217052"
},
{
"type": "event_handler",
"output": {},
"parent": "slot_9_1655398217028",
"context": {
"account_type": "#account_type"
},
"conditions": "#account_type",
"event_name": "input",
"dialog_node": "handler_7_1655398217052"
},
{
"type": "standard",
"title": "business_account",
"output": {
"generic": [
{
"values": [
{
"text": "We have notified your corporate security team, they will be in touch to reset your password."
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": "node_3_1655397279884",
"next_step": {
"behavior": "jump_to",
"selector": "body",
"dialog_node": "node_4_1655399659061"
},
"conditions": "#account_type:business",
"dialog_node": "node_1_1655399028379",
"previous_sibling": "node_3_1655399027429"
},
{
"type": "standard",
"title": "intent_collection",
"output": {
"generic": [
{
"values": [
{
"text": "Thank you for confirming that you want to reset your password."
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"next_step": {
"behavior": "jump_to",
"selector": "body",
"dialog_node": "node_3_1655397279884"
},
"conditions": "#password_reset",
"dialog_node": "node_3_1655396920143",
"previous_sibling": "Welcome"
},
{
"type": "frame",
"title": "account_type_confirmation",
"output": {
"generic": [
{
"values": [
{
"text": "Thank you"
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": "node_3_1655396920143",
"context": {},
"next_step": {
"behavior": "skip_user_input"
},
"conditions": "#password_reset",
"dialog_node": "node_3_1655397279884"
},
{
"type": "standard",
"title": "personal_account",
"output": {
"generic": [
{
"values": [
{
"text": "We have sent you an email with a password reset link."
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"parent": "node_3_1655397279884",
"next_step": {
"behavior": "jump_to",
"selector": "body",
"dialog_node": "node_4_1655399659061"
},
"conditions": "#account_type:personal",
"dialog_node": "node_3_1655399027429"
},
{
"type": "standard",
"title": "reset_confirmation",
"output": {
"generic": [
{
"values": [
{
"text": "Do you need assistance with anything else today?"
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"digress_in": "does_not_return",
"dialog_node": "node_4_1655399659061",
"previous_sibling": "node_3_1655396920143"
},
{
"type": "slot",
"output": {},
"parent": "node_3_1655397279884",
"variable": "$account_type",
"dialog_node": "slot_9_1655398217028",
"previous_sibling": "node_1_1655399028379"
},
{
"type": "standard",
"title": "welcome",
"output": {
"generic": [
{
"values": [
{
"text": "Hello. How can I help you?"
}
],
"response_type": "text",
"selection_policy": "sequential"
}
]
},
"conditions": "welcome",
"dialog_node": "Welcome"
}
],
"counterexamples": [],
"system_settings": {
"off_topic": {
"enabled": true
},
"disambiguation": {
"prompt": "Did you mean:",
"enabled": true,
"randomize": true,
"max_suggestions": 5,
"suggestion_text_policy": "title",
"none_of_the_above_prompt": "None of the above"
},
"human_agent_assist": {
"prompt": "Did you mean:"
},
"intent_classification": {
"training_backend_version": "v2"
},
"spelling_auto_correct": true
},
"learning_opt_out": false,
"name": "Reset Password",
"language": "en",
"description": "Basic Password Reset Request"
}
So what I am missing in my original files, is essentially:
"intents":
and for the entities file:
"entities"
at the start of each list of dictionaries.
Additionally, I would need to wrap the whole thing in curly braces to comply with json formatting.
As seen, the final goal is not just appending these two to one another but the file technically continues with some other JSON code that I have yet to write and deal with.
My question now is as follows; by what method can I either add in these words and the braces to the individual files, then combine them into a singular JSON or alternatively by what method can I read in these files and combine them with the changes all in one go?
The new output file closing on a curly brace after the entities list of dicts is an acceptable outcome for me at the time, so that I can continue to make changes and hopefully further learn from this how to do these changes in future when I get there.
TIA
JSON is only a string format, you can it load in a language structure, in python that is list and dict, do what you need then dump it back, so you don't "add strings" and "add brackets", on modify the structure
file = 'intents.txt'
intents = json.load(open(file)) # load a list
file = 'entities.txt'
entities = json.load(open(file)) # load a list
# create a dict
content = {
"intents": intents,
"entities": entities
}
json.dump(content, open(file, "w"))
If you're reading all the json in as a string, you can just prepend "{'intents':" to the start and append a closing "}".
myJson = "your json string"
myWrappedJson = '{"intents":' + myJson + "}"

Accessing JSON nested object property in Python

I'm loading JSON payload from a stream using json.loads(). I can easily access objects using the following syntax myVar = AgentEvent["EventType"] and I get expected values. However, I'm having problems accessing object named "Contacts" and its properties. For example: How can I access myVar = AgentEvent["CurrentAgentSnapshot"]["Contacts"]["Status"]? When I tried I get an error. Sample JSON below. Any help from Python rock-stars would be much appreciated.
{
"AWSAccountId": "12345678",
"AgentARN": "arn:aws:connect:",
"CurrentAgentSnapshot": {
"AgentStatus": {
"ARN": "arn:aws:connect:",
"Name": "Available",
"StartTimestamp": "2022-03-05T20:35:30.836Z",
"Type": "ROUTABLE"
},
"Configuration": {
"AgentHierarchyGroups": null,
"FirstName": "test",
"LastName": "test",
"RoutingProfile": {
"ARN": "arn:aws:connect:",
"Concurrency": [
{
"AvailableSlots": 0,
"Channel": "CHAT",
"MaximumSlots": 2
},
{
"AvailableSlots": 0,
"Channel": "TASK",
"MaximumSlots": 1
},
{
"AvailableSlots": 0,
"Channel": "VOICE",
"MaximumSlots": 1
}
],
"DefaultOutboundQueue": {
"ARN": "arn:aws:connect:",
"Channels": [
"VOICE"
],
"Name": "BasicQueue"
},
"InboundQueues": [
{
"ARN": "arn:aws:connect:",
"Channels": [
"CHAT",
"TASK",
"VOICE"
],
"Name": "BasicQueue"
},
{
"ARN": "arn:aws:connect:",
"Channels": [
"CHAT",
"TASK",
"VOICE"
],
"Name": null
}
],
"Name": "Basic Routing Profile"
},
"Username": "test"
},
"Contacts": [
{
"Channel": "VOICE",
"ConnectedToAgentTimestamp": "2022-03-05T20:42:14.109Z",
"ContactId": "0a2b3c34-1b7d-4a94-8ff2-e9857d3eac8f",
"InitialContactId": "0a2b3c34-1b7d-4a94-8ff2-e9857d3eac8f",
"InitiationMethod": "INBOUND",
"Queue": {
"ARN": "arn:aws:connect:",
"Name": "BasicQueue"
},
"QueueTimestamp": "2022-03-05T20:42:08.078Z",
"State": "CONNECTED",
"StateStartTimestamp": "2022-03-05T20:51:11.819Z"
}
],
"NextAgentStatus": null
},
"EventId": "ca232cf3-3510-415b-8ff1-8ca89b59194f",
"EventTimestamp": "2022-03-05T21:11:56.315Z",
"EventType": "HEART_BEAT",
"InstanceARN": "arn:aws:connect:",
"PreviousAgentSnapshot": {
"AgentStatus": {
"ARN": "arn:aws:connect:",
"Name": "Available",
"StartTimestamp": "2022-03-05T20:35:30.836Z",
"Type": "ROUTABLE"
},
"Configuration": {
"AgentHierarchyGroups": null,
"FirstName": "test",
"LastName": "test",
"RoutingProfile": {
"ARN": "arn:aws:connect:",
"Concurrency": [
{
"AvailableSlots": 0,
"Channel": "CHAT",
"MaximumSlots": 2
},
{
"AvailableSlots": 0,
"Channel": "TASK",
"MaximumSlots": 1
},
{
"AvailableSlots": 0,
"Channel": "VOICE",
"MaximumSlots": 1
}
],
"DefaultOutboundQueue": {
"ARN": "arn:aws:connect:",
"Channels": [
"VOICE"
],
"Name": "BasicQueue"
},
"InboundQueues": [
{
"ARN": "arn:aws:connect:",
"Channels": [
"CHAT",
"TASK",
"VOICE"
],
"Name": "BasicQueue"
},
{
"ARN": "arn:aws:connect",
"Channels": [
"CHAT",
"TASK",
"VOICE"
],
"Name": null
}
],
"Name": "Basic Routing Profile"
},
"Username": "test"
},
"Contacts": [
{
"Channel": "VOICE",
"ConnectedToAgentTimestamp": "2022-03-05T20:42:14.109Z",
"ContactId": "0a2b3c34-1b7d-4a94-8ff2-e9857d3eac8f",
"InitialContactId": "0a2b3c34-1b7d-4a94-8ff2-e9857d3eac8f",
"InitiationMethod": "INBOUND",
"Queue": {
"ARN": "arn:aws:connect:",
"Name": "BasicQueue"
},
"QueueTimestamp": "2022-03-05T20:42:08.078Z",
"State": "CONNECTED",
"StateStartTimestamp": "2022-03-05T20:51:11.819Z"
}
],
"NextAgentStatus": null
},
"Version": "2017-10-01"}
There are 2 problems with this line
AgentEvent["CurrentAgentSnapshot"]["Contacts"]["Status"]
The Contacts have a State and not a Status
Contacts is a list, not an object. So you need to address its items by index or iterate.
AgentEvent["CurrentAgentSnapshot"]["Contacts"][0]["State"]

JSON manipulation for substituting values python

I have a scenario where I am substituting the values in a payload(data1) by manipulating a json payload(data2).
data2:
[
{
"eqid": 71430,
"tags": [
{
"id": 135853,
"content": "content1",
"class_id": 13733,
"class_name": "reEs"
},
{
"id": 1358341,
"content": "content2",
"class_id": 13734447,
"class_name": "reEp"
},
{
"id": 135832561,
"content": "content3",
"class_id": 137342347,
"class_name": "reEj"
},
{
"id": 1358234561,
"content": "content4",
"class_id": 137123347,
"class_name": "reEk"
},
{
"id": 1355678561,
"content": "content5",
"class_id": 137432347,
"class_name": "reEm"
},
{
"id": 1352348561,
"content": "content6",
"class_id": 137786347,
"class_name": "reEn"
}
],
"auth": false
},
{
"eqid": 243582,
"tags": [
{
"id": 1358456561,
"content": "content1",
"class_id": 137213347,
"class_name": "reEl"
},
{
"id": 13584567561,
"content": "content2",
"class_id": 13745347,
"class_name": "reEt"
},
{
"id": 1353218561,
"content": "content3",
"class_id": 137980347,
"class_name": "reEf"
},
{
"id": 13589758561,
"content": "content4",
"class_id": 1375678347,
"class_name": "reEb"
}
],
"auth": false
},
{
"eqid": 243672,
"tags": [
{
"id": 1358456561,
"content": "content1",
"class_id": 137213347,
"class_name": "reEl"
},
{
"id": 13589758561,
"content": "content4",
"class_id": 1375678347,
"class_name": "reEb"
}
],
"auth": false
}
]
data 1 -
data1 = {
"data": [
{
"name": "app-pp",
"ck_name": "App1",
"eid": 71430,
"Iar": "Osk",
"sps": "Active",
"tgs": "tobedonetages",
"sid": "tobedoneservice",
"last_checked": "19-05-2020"
},
{
"name": "app-pq",
"ck_name": "App2",
"eid": 243582,
"Iar": "Osk",
"sps": "Active",
"tgs": "tobedonetages",
"sid": "tobedoneservice",
"last_checked": "19-05-2020"
}
]
}
Now here based on the condition that if eid of data1 is equal to eqid data2
then replace the value of payload data1 for this two key's tgs & sid with values from data2 of key's content (under tags) and auth.
What I have tried :
for tes in data2:
tempmed = tes["eqid"]
tempservice = tes["auth"]
tempservicel = tes["tags"]
for k in data1:
templand= tempkey["name"]
temphck= tempkey["ck_name"]
tempevalid= tempkey["eid"]
tempiaas= tempkey["Iar"]
tempspc= tempkey["sps"]
temptag= tempkey["tas"]
tempserv= tempkey["sid"]
templc = tempkey["last_checked"]
if tempmed == tempevalid:
tempserv = tempservice
temptag = tempservicel
data1.append({'name': templand, 'ck_name': temphck, 'eid': tempevalid, 'Iar': tempiaas, 'sps': tempspc, 'tgs': temptag, 'sid': tempserv, 'last_checked': templc})
I am not sure what should be the approach to achieve this as the current approach of mine doesn't works as expected.
expected O/P :
{"data":[
{
"name":"app-pp",
"ck_name":"App1",
"eid":71430,
"Iar":"Osk",
"sps":"Active",
"tgs":"content1,content2,content3,content4,content5,content6",
"sid":"false",
"last_checked":"19-05-2020"
},
{
"name":"app-pq",
"ck_name":"App2",
"eid":243582,
"Iar":"Osk",
"sps":"Active",
"tgs":"content1,content2,content3,content4",
"sid":"false",
"last_checked":"19-05-2020"
}
]}
Any help would be great !
It is not optimal but it works. And it can be more readable for beginner.
for item1 in data1['data']:
#print("item1['eid']: ", item1['eid'])
for item2 in data2:
if item1['eid'] == item2['eqid']:
#print("item2['eqid']:", item2['eqid'])
item1['sid'] = item2['auth']
#c = []
#for tag in item2['tags']:
# #print(tag['content'])
# c.append(tag['content'])
#item1['tgs'] = ','.join(c)
item1['tgs'] = ','.join(tag['content'] for tag in item2['tags'])
print(data1)
For bigger data it could be good first to use loop to create structure only with values content and auth from data2 and later use loop to replace it in data1. This way it would run less loops.
Full working example
data2 = [
{
"eqid": 71430,
"tags": [
{
"id": 135853,
"content": "content1",
"class_id": 13733,
"class_name": "reEs"
},
{
"id": 1358341,
"content": "content2",
"class_id": 13734447,
"class_name": "reEp"
},
{
"id": 135832561,
"content": "content3",
"class_id": 137342347,
"class_name": "reEj"
},
{
"id": 1358234561,
"content": "content4",
"class_id": 137123347,
"class_name": "reEk"
},
{
"id": 1355678561,
"content": "content5",
"class_id": 137432347,
"class_name": "reEm"
},
{
"id": 1352348561,
"content": "content6",
"class_id": 137786347,
"class_name": "reEn"
}
],
"auth": False
},
{
"eqid": 243582,
"tags": [
{
"id": 1358456561,
"content": "content1",
"class_id": 137213347,
"class_name": "reEl"
},
{
"id": 13584567561,
"content": "content2",
"class_id": 13745347,
"class_name": "reEt"
},
{
"id": 1353218561,
"content": "content3",
"class_id": 137980347,
"class_name": "reEf"
},
{
"id": 13589758561,
"content": "content4",
"class_id": 1375678347,
"class_name": "reEb"
}
],
"auth": False
},
{
"eqid": 243672,
"tags": [
{
"id": 1358456561,
"content": "content1",
"class_id": 137213347,
"class_name": "reEl"
},
{
"id": 13589758561,
"content": "content4",
"class_id": 1375678347,
"class_name": "reEb"
}
],
"auth": False
}
]
data1 = {
"data": [
{
"name": "app-pp",
"ck_name": "App1",
"eid": 71430,
"Iar": "Osk",
"sps": "Active",
"tgs": "tobedonetages",
"sid": "tobedoneservice",
"last_checked": "19-05-2020"
},
{
"name": "app-pq",
"ck_name": "App2",
"eid": 243582,
"Iar": "Osk",
"sps": "Active",
"tgs": "tobedonetages",
"sid": "tobedoneservice",
"last_checked": "19-05-2020"
}
]
}
for item1 in data1['data']:
print("item1['eid']: ", item1['eid'])
for item2 in data2:
if item1['eid'] == item2['eqid']:
print("item2['eqid']:", item2['eqid'])
item1['sid'] = item2['auth']
c = []
for tag in item2['tags']:
print(tag['content'])
c.append(tag['content'])
c = ','.join(c)
item1['tgs'] = c
print(data1)
try this
pool = {}
for d2 in data2:
tags = d2["tags"]
content = [i["content"] for i in tags]
pool[d2["eqid"]] = [",".join(content), d2["auth"]]
#print(pool)
for i, d1 in enumerate(data1["data"]):
if(d1["eid"] in pool):
data1["data"][i]["tgs"] = pool[d1["eid"]][0]
data1["data"][i]["sid"] = pool[d1["eid"]][1]
print(data1)

Categories