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?










share|improve this question


























    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?










    share|improve this question
























      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 5 at 2:04









      Conando

      1061315




      1061315
























          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 as and bs. This is a great time to use a dictionary mapping the bs to as



          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 as and bs 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 maps



          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





          share|improve this answer























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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
































          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 as and bs. This is a great time to use a dictionary mapping the bs to as



          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 as and bs 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 maps



          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





          share|improve this answer























          • 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















          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 as and bs. This is a great time to use a dictionary mapping the bs to as



          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 as and bs 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 maps



          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





          share|improve this answer























          • 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













          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 as and bs. This is a great time to use a dictionary mapping the bs to as



          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 as and bs 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 maps



          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





          share|improve this answer














          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 as and bs. This is a great time to use a dictionary mapping the bs to as



          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 as and bs 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 maps



          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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




















































































          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini