Is List Comprehension the Right Way to Merge these JSON files Python?
up vote
0
down vote
favorite
How can I use python list comprehension to replace values in one JSON file with linked values in a separate JSON file?
One will look like this and have an "a" value I need to use to replace a value in the other list, using "b" as the connector (a, b and c values are all unique ids):
{
"records":[
{
"a": "7hk2k989u23lesdfsfd",
"b":"b8",
},
{
"a": "9ty562349u23lesdfsfd",
"b":"b6",
},
{
"a": "Ur233Fglesdfsfd",
"b":"b2",
}
]
}
the other will look like this where the "d"s need to be replaced with the corresponding "a" values where "b" is the key:
{
"records":[
{
"c":00023414,
"d":["b8","b6"]
},
{
"c":0005814,
"d":["b8","b2","b6"]
}
]
}
So I end up with:
{
"records":[
{
"c":00023414,
"d":["7hk2k989u23lesdfsfd","9ty562349u23lesdfsfd"]
},
{
"c":0005814,
"d":["7hk2k989u23lesdfsfd","Ur233Fglesdfsfd","9ty562349u23lesdfsfd"]
}
]
}
What's the right way to approach this using python, especially if I need code to be performant?
python json list-comprehension dictionary-comprehension
add a comment |
up vote
0
down vote
favorite
How can I use python list comprehension to replace values in one JSON file with linked values in a separate JSON file?
One will look like this and have an "a" value I need to use to replace a value in the other list, using "b" as the connector (a, b and c values are all unique ids):
{
"records":[
{
"a": "7hk2k989u23lesdfsfd",
"b":"b8",
},
{
"a": "9ty562349u23lesdfsfd",
"b":"b6",
},
{
"a": "Ur233Fglesdfsfd",
"b":"b2",
}
]
}
the other will look like this where the "d"s need to be replaced with the corresponding "a" values where "b" is the key:
{
"records":[
{
"c":00023414,
"d":["b8","b6"]
},
{
"c":0005814,
"d":["b8","b2","b6"]
}
]
}
So I end up with:
{
"records":[
{
"c":00023414,
"d":["7hk2k989u23lesdfsfd","9ty562349u23lesdfsfd"]
},
{
"c":0005814,
"d":["7hk2k989u23lesdfsfd","Ur233Fglesdfsfd","9ty562349u23lesdfsfd"]
}
]
}
What's the right way to approach this using python, especially if I need code to be performant?
python json list-comprehension dictionary-comprehension
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How can I use python list comprehension to replace values in one JSON file with linked values in a separate JSON file?
One will look like this and have an "a" value I need to use to replace a value in the other list, using "b" as the connector (a, b and c values are all unique ids):
{
"records":[
{
"a": "7hk2k989u23lesdfsfd",
"b":"b8",
},
{
"a": "9ty562349u23lesdfsfd",
"b":"b6",
},
{
"a": "Ur233Fglesdfsfd",
"b":"b2",
}
]
}
the other will look like this where the "d"s need to be replaced with the corresponding "a" values where "b" is the key:
{
"records":[
{
"c":00023414,
"d":["b8","b6"]
},
{
"c":0005814,
"d":["b8","b2","b6"]
}
]
}
So I end up with:
{
"records":[
{
"c":00023414,
"d":["7hk2k989u23lesdfsfd","9ty562349u23lesdfsfd"]
},
{
"c":0005814,
"d":["7hk2k989u23lesdfsfd","Ur233Fglesdfsfd","9ty562349u23lesdfsfd"]
}
]
}
What's the right way to approach this using python, especially if I need code to be performant?
python json list-comprehension dictionary-comprehension
How can I use python list comprehension to replace values in one JSON file with linked values in a separate JSON file?
One will look like this and have an "a" value I need to use to replace a value in the other list, using "b" as the connector (a, b and c values are all unique ids):
{
"records":[
{
"a": "7hk2k989u23lesdfsfd",
"b":"b8",
},
{
"a": "9ty562349u23lesdfsfd",
"b":"b6",
},
{
"a": "Ur233Fglesdfsfd",
"b":"b2",
}
]
}
the other will look like this where the "d"s need to be replaced with the corresponding "a" values where "b" is the key:
{
"records":[
{
"c":00023414,
"d":["b8","b6"]
},
{
"c":0005814,
"d":["b8","b2","b6"]
}
]
}
So I end up with:
{
"records":[
{
"c":00023414,
"d":["7hk2k989u23lesdfsfd","9ty562349u23lesdfsfd"]
},
{
"c":0005814,
"d":["7hk2k989u23lesdfsfd","Ur233Fglesdfsfd","9ty562349u23lesdfsfd"]
}
]
}
What's the right way to approach this using python, especially if I need code to be performant?
python json list-comprehension dictionary-comprehension
python json list-comprehension dictionary-comprehension
asked Nov 5 at 2:04
Conando
1061315
1061315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Your files aren't valid JSON. You should check with a JSON validator like JSON Lint
In [494]: import json
In [495]: with open('/Users/ado/Desktop/ab.json') as f:
...: ab = json.load(f)
...:
In [496]: with open('/Users/ado/Desktop/cd.json') as f:
...: cd = json.load(f)
...:
Notice that you could look at ab
simply as a collection of related a
s and b
s. This is a great time to use a dictionary mapping the b
s to a
s
In [497]: d_ab = {r['b']: r['a'] for r in ab['records']}
In [498]: d_ab
Out[498]:
{'b2': 'Ur233Fglesdfsfd',
'b6': '9ty562349u23lesdfsfd',
'b8': '7hk2k989u23lesdfsfd'}
Now you can iterate over the records
in cd
and use a list
comprehension to create the new values
In [499]: for r in cd['records']:
...: r['d'] = [d_ab.get(d) for d in r['d']]
...:
In [500]: cd
Out[500]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
Finally, write the new contents to file
In [502]: with open('/Users/ado/Desktop/cd-mapped.json', 'w') as f:
...: f.write(json.dumps(cd))
...:
This solution presupposes that in ab
there is always a
s and b
s in every record.
PS for funsies, you could use map
and dict.get
instead of the comprehension
In [505]: for r in cd['records']:
...: r['d'] = list(map(d_ab.get, r['d']))
...:
In [506]: cd
Out[506]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
As far as performance goes, comprehensions often edge out map
s
In [509]: %timeit for r in cd['records']: r['d'] = [d_ab.get(d) for d in r['d']]
...:
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.34 µs per loop
In [511]: %timeit for r in cd['records']: r['d'] = list(map(d_ab.get, r['d']))
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.74 µs per loop
Wow, that's a really helpful insight aydow, turning a and b into key:value! Lot's to learn from in your examples.
– Conando
Nov 6 at 1:02
list comprehension is an amazing feature of python
– Conando
Nov 6 at 1:19
thanks @Conando :) if the answer helped you please consider upvoting and accepting
– aydow
Nov 6 at 1:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Your files aren't valid JSON. You should check with a JSON validator like JSON Lint
In [494]: import json
In [495]: with open('/Users/ado/Desktop/ab.json') as f:
...: ab = json.load(f)
...:
In [496]: with open('/Users/ado/Desktop/cd.json') as f:
...: cd = json.load(f)
...:
Notice that you could look at ab
simply as a collection of related a
s and b
s. This is a great time to use a dictionary mapping the b
s to a
s
In [497]: d_ab = {r['b']: r['a'] for r in ab['records']}
In [498]: d_ab
Out[498]:
{'b2': 'Ur233Fglesdfsfd',
'b6': '9ty562349u23lesdfsfd',
'b8': '7hk2k989u23lesdfsfd'}
Now you can iterate over the records
in cd
and use a list
comprehension to create the new values
In [499]: for r in cd['records']:
...: r['d'] = [d_ab.get(d) for d in r['d']]
...:
In [500]: cd
Out[500]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
Finally, write the new contents to file
In [502]: with open('/Users/ado/Desktop/cd-mapped.json', 'w') as f:
...: f.write(json.dumps(cd))
...:
This solution presupposes that in ab
there is always a
s and b
s in every record.
PS for funsies, you could use map
and dict.get
instead of the comprehension
In [505]: for r in cd['records']:
...: r['d'] = list(map(d_ab.get, r['d']))
...:
In [506]: cd
Out[506]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
As far as performance goes, comprehensions often edge out map
s
In [509]: %timeit for r in cd['records']: r['d'] = [d_ab.get(d) for d in r['d']]
...:
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.34 µs per loop
In [511]: %timeit for r in cd['records']: r['d'] = list(map(d_ab.get, r['d']))
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.74 µs per loop
Wow, that's a really helpful insight aydow, turning a and b into key:value! Lot's to learn from in your examples.
– Conando
Nov 6 at 1:02
list comprehension is an amazing feature of python
– Conando
Nov 6 at 1:19
thanks @Conando :) if the answer helped you please consider upvoting and accepting
– aydow
Nov 6 at 1:58
add a comment |
up vote
2
down vote
accepted
Your files aren't valid JSON. You should check with a JSON validator like JSON Lint
In [494]: import json
In [495]: with open('/Users/ado/Desktop/ab.json') as f:
...: ab = json.load(f)
...:
In [496]: with open('/Users/ado/Desktop/cd.json') as f:
...: cd = json.load(f)
...:
Notice that you could look at ab
simply as a collection of related a
s and b
s. This is a great time to use a dictionary mapping the b
s to a
s
In [497]: d_ab = {r['b']: r['a'] for r in ab['records']}
In [498]: d_ab
Out[498]:
{'b2': 'Ur233Fglesdfsfd',
'b6': '9ty562349u23lesdfsfd',
'b8': '7hk2k989u23lesdfsfd'}
Now you can iterate over the records
in cd
and use a list
comprehension to create the new values
In [499]: for r in cd['records']:
...: r['d'] = [d_ab.get(d) for d in r['d']]
...:
In [500]: cd
Out[500]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
Finally, write the new contents to file
In [502]: with open('/Users/ado/Desktop/cd-mapped.json', 'w') as f:
...: f.write(json.dumps(cd))
...:
This solution presupposes that in ab
there is always a
s and b
s in every record.
PS for funsies, you could use map
and dict.get
instead of the comprehension
In [505]: for r in cd['records']:
...: r['d'] = list(map(d_ab.get, r['d']))
...:
In [506]: cd
Out[506]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
As far as performance goes, comprehensions often edge out map
s
In [509]: %timeit for r in cd['records']: r['d'] = [d_ab.get(d) for d in r['d']]
...:
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.34 µs per loop
In [511]: %timeit for r in cd['records']: r['d'] = list(map(d_ab.get, r['d']))
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.74 µs per loop
Wow, that's a really helpful insight aydow, turning a and b into key:value! Lot's to learn from in your examples.
– Conando
Nov 6 at 1:02
list comprehension is an amazing feature of python
– Conando
Nov 6 at 1:19
thanks @Conando :) if the answer helped you please consider upvoting and accepting
– aydow
Nov 6 at 1:58
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Your files aren't valid JSON. You should check with a JSON validator like JSON Lint
In [494]: import json
In [495]: with open('/Users/ado/Desktop/ab.json') as f:
...: ab = json.load(f)
...:
In [496]: with open('/Users/ado/Desktop/cd.json') as f:
...: cd = json.load(f)
...:
Notice that you could look at ab
simply as a collection of related a
s and b
s. This is a great time to use a dictionary mapping the b
s to a
s
In [497]: d_ab = {r['b']: r['a'] for r in ab['records']}
In [498]: d_ab
Out[498]:
{'b2': 'Ur233Fglesdfsfd',
'b6': '9ty562349u23lesdfsfd',
'b8': '7hk2k989u23lesdfsfd'}
Now you can iterate over the records
in cd
and use a list
comprehension to create the new values
In [499]: for r in cd['records']:
...: r['d'] = [d_ab.get(d) for d in r['d']]
...:
In [500]: cd
Out[500]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
Finally, write the new contents to file
In [502]: with open('/Users/ado/Desktop/cd-mapped.json', 'w') as f:
...: f.write(json.dumps(cd))
...:
This solution presupposes that in ab
there is always a
s and b
s in every record.
PS for funsies, you could use map
and dict.get
instead of the comprehension
In [505]: for r in cd['records']:
...: r['d'] = list(map(d_ab.get, r['d']))
...:
In [506]: cd
Out[506]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
As far as performance goes, comprehensions often edge out map
s
In [509]: %timeit for r in cd['records']: r['d'] = [d_ab.get(d) for d in r['d']]
...:
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.34 µs per loop
In [511]: %timeit for r in cd['records']: r['d'] = list(map(d_ab.get, r['d']))
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.74 µs per loop
Your files aren't valid JSON. You should check with a JSON validator like JSON Lint
In [494]: import json
In [495]: with open('/Users/ado/Desktop/ab.json') as f:
...: ab = json.load(f)
...:
In [496]: with open('/Users/ado/Desktop/cd.json') as f:
...: cd = json.load(f)
...:
Notice that you could look at ab
simply as a collection of related a
s and b
s. This is a great time to use a dictionary mapping the b
s to a
s
In [497]: d_ab = {r['b']: r['a'] for r in ab['records']}
In [498]: d_ab
Out[498]:
{'b2': 'Ur233Fglesdfsfd',
'b6': '9ty562349u23lesdfsfd',
'b8': '7hk2k989u23lesdfsfd'}
Now you can iterate over the records
in cd
and use a list
comprehension to create the new values
In [499]: for r in cd['records']:
...: r['d'] = [d_ab.get(d) for d in r['d']]
...:
In [500]: cd
Out[500]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
Finally, write the new contents to file
In [502]: with open('/Users/ado/Desktop/cd-mapped.json', 'w') as f:
...: f.write(json.dumps(cd))
...:
This solution presupposes that in ab
there is always a
s and b
s in every record.
PS for funsies, you could use map
and dict.get
instead of the comprehension
In [505]: for r in cd['records']:
...: r['d'] = list(map(d_ab.get, r['d']))
...:
In [506]: cd
Out[506]:
{'records': [{'c': 23414,
'd': ['7hk2k989u23lesdfsfd', '9ty562349u23lesdfsfd']},
{'c': 5814,
'd': ['7hk2k989u23lesdfsfd', 'Ur233Fglesdfsfd', '9ty562349u23lesdfsfd']}]}
As far as performance goes, comprehensions often edge out map
s
In [509]: %timeit for r in cd['records']: r['d'] = [d_ab.get(d) for d in r['d']]
...:
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.34 µs per loop
In [511]: %timeit for r in cd['records']: r['d'] = list(map(d_ab.get, r['d']))
The slowest run took 7.19 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.74 µs per loop
edited Nov 5 at 2:54
answered Nov 5 at 2:41
aydow
2,10711025
2,10711025
Wow, that's a really helpful insight aydow, turning a and b into key:value! Lot's to learn from in your examples.
– Conando
Nov 6 at 1:02
list comprehension is an amazing feature of python
– Conando
Nov 6 at 1:19
thanks @Conando :) if the answer helped you please consider upvoting and accepting
– aydow
Nov 6 at 1:58
add a comment |
Wow, that's a really helpful insight aydow, turning a and b into key:value! Lot's to learn from in your examples.
– Conando
Nov 6 at 1:02
list comprehension is an amazing feature of python
– Conando
Nov 6 at 1:19
thanks @Conando :) if the answer helped you please consider upvoting and accepting
– aydow
Nov 6 at 1:58
Wow, that's a really helpful insight aydow, turning a and b into key:value! Lot's to learn from in your examples.
– Conando
Nov 6 at 1:02
Wow, that's a really helpful insight aydow, turning a and b into key:value! Lot's to learn from in your examples.
– Conando
Nov 6 at 1:02
list comprehension is an amazing feature of python
– Conando
Nov 6 at 1:19
list comprehension is an amazing feature of python
– Conando
Nov 6 at 1:19
thanks @Conando :) if the answer helped you please consider upvoting and accepting
– aydow
Nov 6 at 1:58
thanks @Conando :) if the answer helped you please consider upvoting and accepting
– aydow
Nov 6 at 1:58
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147397%2fis-list-comprehension-the-right-way-to-merge-these-json-files-python%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password