I have two json files with a key,value in common, and I would like to combine them making sure that the information of one slots into the other correctly.
So I have:
[
{
["id": "fc4f",
"data": [
{
"spk": "A",
"utt": "Hello.",
"meta": "abdbcbsb",
"words": "sbdbe"
},
{
"spk": "B",
"utt": "Hello, how can I help you?",
"meta": "etc etc",
"words": "demdk"
}]
},
{"id": "fc5f",
"data": [
{
"spk": "A",
"utt": "Okay then.",
"meta": "de fw",
"words": "dwerbj"
},
{
"spk": "B",
"utt": "Going on",
"meta": "etc2 etc2",
"words": "felm"
}]
}
]
and the second file is like this, where the "id" and "utt" keys are in common:
[
{
["id": "fc4f",
"data": [
{
"utt": "Hello.",
"number": "112"
},
{
"utt": "Hello, how can I help you?",
"number": "113"
}]
},
{"id": "fc5f",
"data": [
{
"utt": "Okay then.",
"number": "11"
},
{
"utt": "Going on",
"number": "1"
}]
}
]
And the result would ideally look like:
[
{
["id": "fc4f",
"data": [
{
"spk": "A",
"utt": "Hello.",
"meta": "abdbcbsb",
"words": "sbdbe",
"number": "112"
},
{
"spk": "B",
"utt": "Hello, how can I help you?",
"meta": "etc etc",
"words": "demdk",
"number": "113"
}]
},
{"id": "fc5f",
"data": [
{
"spk": "A",
"utt": "Okay then.",
"meta": "de fw",
"words": "dwerbj",
"number": "1"
},
{
"spk": "B",
"utt": "Going on",
"meta": "etc2 etc2",
"words": "felm"
"number": "11"
}]
}
]
Because file2 might not have all the data in file1, I would need to make sure to skip those instances where the data doesn't match. I start with loading both files with json.load() but then I don't know how to go on. Can someone help?
Related
Hi I have two dictionaries 1.Primary, 2. Secondary
Need to check first field of both dictionary
If field is same compare the title with primary and secondary
*If field and title is same then From Primary dictionary add count to secondary dictionary
Primary dictionary
{"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"count": "1"
},
{
"title": "C",
"paragraph": "null",
"count": "1"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"count": "3"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"count": "1"
}
]
}
]}
Secondary dictionary
[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"count": "null"
},
{
"title": "B",
"paragraph": "null",
"count": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "test",
"count": "null"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Tester",
"paragraph": "null",
"count": "null"
}
]
}
]
Expected out
{"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"count": "1"
},
{
"title": "C",
"paragraph": "null",
"count": "1"
},
{
"title": "B",
"paragraph": "null",
"count": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"count": "3"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"count": "1"
},
{
"title": "Tester",
"paragraph": "null"
"count": "null"
}
]
}
]}
COde
for primary_elem in primary['Latest']:
primary_titles = [value['title'] for value in primary_elem['values']]
for secondary_elem in secondary:
if secondary_elem['field'] == primary_elem['field']:
for secondary_value in secondary_elem['values']:
if secondary_value['title'] in primary_titles:
for value in primary_elem['values']:
if secondary_value['title'] == value['title']:
secondary_value['count'] = value['count']
got error string element must be integers
How to reduce one loop also 6 to 5
Change
for primary_elem in primary['Latest']:
primary_titles = [value['title'] for value in primary_elem['values']]
for secondary_elem in secondary:
if secondary_elem['field'] == primary_elem['field']:
for secondary_value in secondary_elem['values']:
if secondary_value['title'] in primary_titles:
secondary_elem['count'] = primary_elem['count']
To
for buttfart_junior in primary['Latest']:
buttfart_senior = [value['title'] for value in buttfart_junior['values']]
for young_buttfart in secondary:
if young_buttfart['field'] == buttfart_senior['field']:
for buttfart_farts in buttfart_junior['values']:
if buttfart_farts['title'] in buttfart_senior:
buttfart_junior['count'] = buttfart_senior['count']
Hi I have two dictionaries 1.Primary, 2. Secondary
Need to check first field of both dictionary
If filed is same compare the title with primary and secondary
if Primary have missing dictionary which is in dictionary add that to primary
Primary dictionary
{"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"role": "Deveoper"
},
{
"title": "C",
"paragraph": "null",
"role": "Tester"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"role": "Long Term"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"role": "null"
}
]
}
]}
Secondary dictionary
[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"role": "null"
},
{
"title": "B",
"paragraph": "null",
"role": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "test",
"role": "null"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Tester",
"paragraph": "null",
"role": "null"
}
]
}
]
Expected out
{"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"role": "Deveoper"
},
{
"title": "C",
"paragraph": "null",
"role": "Tester"
},
{
"title": "B",
"paragraph": "null",
"role": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"role": "Long Term"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"role": "null"
},
{
"title": "Tester",
"paragraph": "null"
"role": "null"
}
]
}
]}
COde
for i in primary['Latest']:
for j in secondary:
if i['field'] == j['field']:
for a in i['values']:
for b in j['values']:
if a['title'] == b['title']:
i['values'].append(b)
My code executes indefinitely
The issue is that you are appending in i['values'] while iterating on it, creating unexpected behaviour. You can fix it by iterating on a "frozen" list if i['values'] like this:
from copy import deepcopy
for i in primary['Latest']:
for j in secondary:
if i['field'] == j['field']:
values = deepcopy(i['values'])
for a in values:
for b in j['values']:
if a['title'] == b['title']:
i['values'].append(b)
This resolves the "indefinitely" part, but doesn't quite work, here is the output:
print(json.dumps(primary, indent=2))
{
"Latest": [
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": null,
"role": "Deveoper"
},
{
"title": "C",
"paragraph": null,
"role": "Tester"
},
{
"title": "A",
"paragraph": null,
"role": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"role": "Long Term"
},
{
"title": "NEW_York",
"paragraph": "test",
"role": "null"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": null,
"role": "null"
}
]
}
]
}
So I fixed it a little:
for primary_elem in primary['Latest']:
primary_titles = [value['title'] for value in primary_elem['values']]
for secondary_elem in secondary:
if secondary_elem['field'] == primary_elem['field']:
for secondary_value in secondary_elem['values']:
if secondary_value['title'] not in primary_titles:
primary_elem['values'].append(secondary_value)
Which gives
>>> print(json.dumps(primary, indent=2))
{
"Latest": [
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": null,
"role": "Deveoper"
},
{
"title": "C",
"paragraph": null,
"role": "Tester"
},
{
"title": "B",
"paragraph": null,
"role": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"role": "Long Term"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": null,
"role": "null"
},
{
"title": "Tester",
"paragraph": null,
"role": "null"
}
]
}
]
}
{
"type": "Data",
"version": "1.0",
"box": {
"identifier": "abcdef",
"serial": "12345678"
},
"payload": {
"Type": "EL",
"Version": "1",
"Result": "Successful",
"Reference": null,
"Box": {
"Identifier": "abcdef",
"Serial": "12345678"
},
"Configuration": {
"EL": "1"
},
"vent": [
{
"ventType": "Arm",
"Timestamp": "2020-03-18T12:17:04+10:00",
"Parameters": [
{
"Name": "Arm",
"Value": "LT"
},
{
"Name": "Status",
"Value": "LD"
}
]
},
{
"ventType": "Arm",
"Timestamp": "2020-03-18T12:17:24+10:00",
"Parameters": [
{
"Name": "Arm",
"Value": "LT"
},
{
"Name": "Status",
"Value": "LD"
}
]
},
{
"EventType": "TimeUpdateCompleted",
"Timestamp": "2020-03-18T02:23:21.2979668Z",
"Parameters": [
{
"Name": "ActualAdjustment",
"Value": "PT0S"
},
{
"Name": "CorrectionOffset",
"Value": "PT0S"
},
{
"Name": "Latency",
"Value": "PT0.2423996S"
}
]
}
]
}
}
If you're looking to transfer information from a JSON file to a CSV, then you can use the following code to read in a JSON file into a dictionary in Python:
import json
with open('data.txt') as json_file:
data_dict = json.load(json_file)
You could then convert this dictionary into a list with either data_dict.items() or data_dict.values().
Then you just need to write this list to a CSV file which you can easily do by just looping through the list.
I am new to python. I want to extract the elements of array 'Address' from below json. I am trying to use map to split the elements using
r=<below json>
s=r["Customer"]
y=s.map(lambda x:x.split(","))
But I am getting the error as .....AttributeError: 'str' object has no attribute 'map'
Can you please advise which is the best way to do this.
{ "id": "981",
"Customer":
[
{
"Name": "abc",
"Title": "Mr",
"Gender": "M",
"Address": [
{
"Postcode": "2GF",
"StreetName": "Black Street",
"FlatNo": "123",
}
]
},
{
"Name": "xyz",
"Title": "Mrs",
"Gender": "F",
"Address": [
{
"Postcode": "2CX",
"StreetName": "White Street",
"FlatNo": "123",
}
]
}
]
}
Why not just:
data = { id: 981,
'Customer':
[
{
"Name": "abc",
"Title": "Mr",
"Gender": "M",
"Address": [
{
"Postcode": "2GF",
"StreetName": "Black Street",
"FlatNo": "123",
}
]
},
{
"Name": "xyz",
"Title": "Mrs",
"Gender": "F",
"Address": [
{
"Postcode": "2CX",
"StreetName": "White Street",
"FlatNo": "123",
}
]
}
]
}
for item in data['Customer']:
for data_item in item['Address']:
print(data_item['Postcode'], data_item['StreetName'], data_item['FlatNo'])
OUTPUT:
2GF Black Street 123
2CX White Street 123
"I want to extract the elements of array 'Address' from below json":
[x for dd in r['Customer'] for x in dd['Address']]
Actually I build Json object starting from a python object.
My starting JSON is:
responseMsgObject = {'Version': 1,
'Id': 'xc23',
'Local': "US"
'Type': "Test",
'Message' : "Message body" }
responseMsgJson = json.dumps(responseMsgObject, sort_keys=False )
Every things works but now I need to put the JSON below into "Message" field.
{
"DepID": "001",
"Assets": [
{
"Type": "xyz",
"Text": [
"abc",
"def"
],
"Metadata": {
"V": "1",
"Req": true,
"Other": "othervalue"
},
"Check": "refdw321"
},
{
"Type": "jkl",
"Text": [
"ghi"
],
"Metadata": {
"V": "6"
},
"Check": "345ghsdan"
}
]
}
I built many other json (but simpler) but I'm in trouble with this json.
Thanks for the help.
try to replace true with True works fine for me
import json
responseMsgObject = {
'Version': 1,
'Id': 'xc23',
'Local': "US",
'Type': "Test",
'Message': {
"DepID": "001",
"Assets": [{
"Type": "xyz",
"Text": [
"abc",
"def"
],
"Metadata": {
"V": "1",
"Req": True,
"Other": "othervalue"
},
"Check": "refdw321"
}, {
"Type": "jkl",
"Text": [
"ghi"
],
"Metadata": {
"V": "6"
},
"Check": "345ghsdan4"
}]
}
}
responseMsgJson = json.dumps(responseMsgObject, sort_keys=False )
print("responseMsgJson", responseMsgJson)
DEMO