Create json from object where keys contain '.'












-3















How can I convert



 {'Address.street': 's street',
'Address.streetNum': 's street num',
'Address.npa': 'npa',
'Address.city': 's city',
'Address.country': 's country'}


to



{
Address:{
street: 's street',
streetNum: 's street num'
npa: 'npa'
city: 's city',
country: 's country'
}
}


I am using nodejs (v10.12.0) for backend.



I have trid to use lodash pick, but is not working as expected.



any idea ?










share|improve this question




















  • 3





    these look like JavaScript objects, not JSON strings. Anyway, read the object keys, split them (they're strings) by the dot, and then use the second part as the key names for your inner object. What precisely have you researched or tried so far? The logic doesn't seem hard.

    – ADyson
    Nov 14 '18 at 13:39








  • 1





    Possible duplicate of Convert javascript dot notation object to nested object

    – str
    Nov 14 '18 at 13:58
















-3















How can I convert



 {'Address.street': 's street',
'Address.streetNum': 's street num',
'Address.npa': 'npa',
'Address.city': 's city',
'Address.country': 's country'}


to



{
Address:{
street: 's street',
streetNum: 's street num'
npa: 'npa'
city: 's city',
country: 's country'
}
}


I am using nodejs (v10.12.0) for backend.



I have trid to use lodash pick, but is not working as expected.



any idea ?










share|improve this question




















  • 3





    these look like JavaScript objects, not JSON strings. Anyway, read the object keys, split them (they're strings) by the dot, and then use the second part as the key names for your inner object. What precisely have you researched or tried so far? The logic doesn't seem hard.

    – ADyson
    Nov 14 '18 at 13:39








  • 1





    Possible duplicate of Convert javascript dot notation object to nested object

    – str
    Nov 14 '18 at 13:58














-3












-3








-3








How can I convert



 {'Address.street': 's street',
'Address.streetNum': 's street num',
'Address.npa': 'npa',
'Address.city': 's city',
'Address.country': 's country'}


to



{
Address:{
street: 's street',
streetNum: 's street num'
npa: 'npa'
city: 's city',
country: 's country'
}
}


I am using nodejs (v10.12.0) for backend.



I have trid to use lodash pick, but is not working as expected.



any idea ?










share|improve this question
















How can I convert



 {'Address.street': 's street',
'Address.streetNum': 's street num',
'Address.npa': 'npa',
'Address.city': 's city',
'Address.country': 's country'}


to



{
Address:{
street: 's street',
streetNum: 's street num'
npa: 'npa'
city: 's city',
country: 's country'
}
}


I am using nodejs (v10.12.0) for backend.



I have trid to use lodash pick, but is not working as expected.



any idea ?







javascript node.js json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 13:39









ADyson

24k112445




24k112445










asked Nov 14 '18 at 13:38









dmxdmx

621725




621725








  • 3





    these look like JavaScript objects, not JSON strings. Anyway, read the object keys, split them (they're strings) by the dot, and then use the second part as the key names for your inner object. What precisely have you researched or tried so far? The logic doesn't seem hard.

    – ADyson
    Nov 14 '18 at 13:39








  • 1





    Possible duplicate of Convert javascript dot notation object to nested object

    – str
    Nov 14 '18 at 13:58














  • 3





    these look like JavaScript objects, not JSON strings. Anyway, read the object keys, split them (they're strings) by the dot, and then use the second part as the key names for your inner object. What precisely have you researched or tried so far? The logic doesn't seem hard.

    – ADyson
    Nov 14 '18 at 13:39








  • 1





    Possible duplicate of Convert javascript dot notation object to nested object

    – str
    Nov 14 '18 at 13:58








3




3





these look like JavaScript objects, not JSON strings. Anyway, read the object keys, split them (they're strings) by the dot, and then use the second part as the key names for your inner object. What precisely have you researched or tried so far? The logic doesn't seem hard.

– ADyson
Nov 14 '18 at 13:39







these look like JavaScript objects, not JSON strings. Anyway, read the object keys, split them (they're strings) by the dot, and then use the second part as the key names for your inner object. What precisely have you researched or tried so far? The logic doesn't seem hard.

– ADyson
Nov 14 '18 at 13:39






1




1





Possible duplicate of Convert javascript dot notation object to nested object

– str
Nov 14 '18 at 13:58





Possible duplicate of Convert javascript dot notation object to nested object

– str
Nov 14 '18 at 13:58












2 Answers
2






active

oldest

votes


















0














for this create blank array then assign blank object
then push the object in array



let address=
let object=Object.assign({});
object.street='s street';
object.streetNum='s street num'
....

address.push(object)

you can console.log(address)
you will achieve the result





share|improve this answer































    0














    for a more general solution, here's something I quickly wrote up:



    var unflatten = function unflatten(obj) {
    var ret = {};

    //for each flattened key in our original object
    for(var key in obj) {
    if(obj.hasOwnProperty(key)) {
    //split each key into parts separated by '.'
    var parts = key.split('.');

    //keep the last part separate, we'll need it assign our value
    var lastPart = parts.pop();

    //keep a reference to the current child object
    var parent = ret;

    //for each part in our key
    for(var i = 0; i < parts.length; i++) {
    var part = parts[i];

    //only create a new object if it doesn't exist
    if(!parent[part])
    parent[part] = {};

    //reassign our parent to go one level deeper
    parent = parent[part];
    }

    //finally, assign our original value
    //note that this will overwrite whatever's already there
    parent[lastPart] = obj[key];
    }
    }

    return ret;
    };

    //usage:
    var obj = {
    'foo.bar.baz': 1,
    'foo.qux': 2
    };

    unflatten(obj);

    //returns { foo: { bar: { baz: 1 }, qux: 2 } }


    see also @str's comment for other solutions/discussion






    share|improve this answer























      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',
      autoActivateHeartbeat: false,
      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%2f53301565%2fcreate-json-from-object-where-keys-contain%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      for this create blank array then assign blank object
      then push the object in array



      let address=
      let object=Object.assign({});
      object.street='s street';
      object.streetNum='s street num'
      ....

      address.push(object)

      you can console.log(address)
      you will achieve the result





      share|improve this answer




























        0














        for this create blank array then assign blank object
        then push the object in array



        let address=
        let object=Object.assign({});
        object.street='s street';
        object.streetNum='s street num'
        ....

        address.push(object)

        you can console.log(address)
        you will achieve the result





        share|improve this answer


























          0












          0








          0







          for this create blank array then assign blank object
          then push the object in array



          let address=
          let object=Object.assign({});
          object.street='s street';
          object.streetNum='s street num'
          ....

          address.push(object)

          you can console.log(address)
          you will achieve the result





          share|improve this answer













          for this create blank array then assign blank object
          then push the object in array



          let address=
          let object=Object.assign({});
          object.street='s street';
          object.streetNum='s street num'
          ....

          address.push(object)

          you can console.log(address)
          you will achieve the result






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 13:51









          Subhash ViswakarmaSubhash Viswakarma

          792




          792

























              0














              for a more general solution, here's something I quickly wrote up:



              var unflatten = function unflatten(obj) {
              var ret = {};

              //for each flattened key in our original object
              for(var key in obj) {
              if(obj.hasOwnProperty(key)) {
              //split each key into parts separated by '.'
              var parts = key.split('.');

              //keep the last part separate, we'll need it assign our value
              var lastPart = parts.pop();

              //keep a reference to the current child object
              var parent = ret;

              //for each part in our key
              for(var i = 0; i < parts.length; i++) {
              var part = parts[i];

              //only create a new object if it doesn't exist
              if(!parent[part])
              parent[part] = {};

              //reassign our parent to go one level deeper
              parent = parent[part];
              }

              //finally, assign our original value
              //note that this will overwrite whatever's already there
              parent[lastPart] = obj[key];
              }
              }

              return ret;
              };

              //usage:
              var obj = {
              'foo.bar.baz': 1,
              'foo.qux': 2
              };

              unflatten(obj);

              //returns { foo: { bar: { baz: 1 }, qux: 2 } }


              see also @str's comment for other solutions/discussion






              share|improve this answer




























                0














                for a more general solution, here's something I quickly wrote up:



                var unflatten = function unflatten(obj) {
                var ret = {};

                //for each flattened key in our original object
                for(var key in obj) {
                if(obj.hasOwnProperty(key)) {
                //split each key into parts separated by '.'
                var parts = key.split('.');

                //keep the last part separate, we'll need it assign our value
                var lastPart = parts.pop();

                //keep a reference to the current child object
                var parent = ret;

                //for each part in our key
                for(var i = 0; i < parts.length; i++) {
                var part = parts[i];

                //only create a new object if it doesn't exist
                if(!parent[part])
                parent[part] = {};

                //reassign our parent to go one level deeper
                parent = parent[part];
                }

                //finally, assign our original value
                //note that this will overwrite whatever's already there
                parent[lastPart] = obj[key];
                }
                }

                return ret;
                };

                //usage:
                var obj = {
                'foo.bar.baz': 1,
                'foo.qux': 2
                };

                unflatten(obj);

                //returns { foo: { bar: { baz: 1 }, qux: 2 } }


                see also @str's comment for other solutions/discussion






                share|improve this answer


























                  0












                  0








                  0







                  for a more general solution, here's something I quickly wrote up:



                  var unflatten = function unflatten(obj) {
                  var ret = {};

                  //for each flattened key in our original object
                  for(var key in obj) {
                  if(obj.hasOwnProperty(key)) {
                  //split each key into parts separated by '.'
                  var parts = key.split('.');

                  //keep the last part separate, we'll need it assign our value
                  var lastPart = parts.pop();

                  //keep a reference to the current child object
                  var parent = ret;

                  //for each part in our key
                  for(var i = 0; i < parts.length; i++) {
                  var part = parts[i];

                  //only create a new object if it doesn't exist
                  if(!parent[part])
                  parent[part] = {};

                  //reassign our parent to go one level deeper
                  parent = parent[part];
                  }

                  //finally, assign our original value
                  //note that this will overwrite whatever's already there
                  parent[lastPart] = obj[key];
                  }
                  }

                  return ret;
                  };

                  //usage:
                  var obj = {
                  'foo.bar.baz': 1,
                  'foo.qux': 2
                  };

                  unflatten(obj);

                  //returns { foo: { bar: { baz: 1 }, qux: 2 } }


                  see also @str's comment for other solutions/discussion






                  share|improve this answer













                  for a more general solution, here's something I quickly wrote up:



                  var unflatten = function unflatten(obj) {
                  var ret = {};

                  //for each flattened key in our original object
                  for(var key in obj) {
                  if(obj.hasOwnProperty(key)) {
                  //split each key into parts separated by '.'
                  var parts = key.split('.');

                  //keep the last part separate, we'll need it assign our value
                  var lastPart = parts.pop();

                  //keep a reference to the current child object
                  var parent = ret;

                  //for each part in our key
                  for(var i = 0; i < parts.length; i++) {
                  var part = parts[i];

                  //only create a new object if it doesn't exist
                  if(!parent[part])
                  parent[part] = {};

                  //reassign our parent to go one level deeper
                  parent = parent[part];
                  }

                  //finally, assign our original value
                  //note that this will overwrite whatever's already there
                  parent[lastPart] = obj[key];
                  }
                  }

                  return ret;
                  };

                  //usage:
                  var obj = {
                  'foo.bar.baz': 1,
                  'foo.qux': 2
                  };

                  unflatten(obj);

                  //returns { foo: { bar: { baz: 1 }, qux: 2 } }


                  see also @str's comment for other solutions/discussion







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 14:08









                  Andrew AultAndrew Ault

                  34126




                  34126






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301565%2fcreate-json-from-object-where-keys-contain%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly