1

I am a beginner and am trying to create the following JSON From data extraction of HTML input texts. How can I create and access the following JSON?

{  
  Brazilians**{
    John**{
      old: 18
    }

    Jones**{
      old: 20
    }
  }

  Canadians**{

    Friedman**{
      old: 22
    }

    James**{
      old: 35
    }
  }
}

Note: ** Means it must be a dynamic key.

My HTML code:

<input type="text" id="nacionality">
<input type="text" id="name">
<input type="text" id="old">

The structure I tried to develop in javascript:

obj[nationality] = name[{old : oldValue}];

alert(obj["Brazilians"]["John"].old);
3
  • Your json is wrong..code works fine. Commented Feb 9, 2017 at 14:53
  • Could you help me? Commented Feb 9, 2017 at 14:55
  • What is your use case? Commented Feb 9, 2017 at 15:03

4 Answers 4

5

Something like this?

var obj = {};
obj[document.getElementById('nationality').value] = {};
obj[document.getElementById('nationality').value][document.getElementById('name').value] = {
  old: document.getElementById('old').value
};
console.log(obj);
<input type="text" id="nationality" value="Brazilian" />
<input type="text" id="name" value="John" />
<input type="text" id="old" value="18" />

Sign up to request clarification or add additional context in comments.

3 Comments

Just a side-note it's nacionality - from the original question.
Right. But that word doesn't exist in English.
old and name are English words, so I assumed nationality was the word he meant to write
3

You should create a new object with your details.You can create this using Immediately-invoked function expression.

obj[nationality]=(function(){
   var obj1={};
   obj1[name]={
       old:parseInt(old)
   };
   return obj1;
})();

var obj={  
  Brazilians:{
    John:{
      old: 18
    }
    ,
    Jones:{
      old: 20
    }
  }
  ,
  Canadians:{

    Friedman:{
      old: 22
    }
    ,
    James:{
      old: 35
    }
  }
}
function add(){
   var nationality=document.getElementById('nacionality').value;
  var name=document.getElementById('name').value;
  var old=document.getElementById('old').value;
  obj[nationality]=(function(){
      var obj1={};
      obj1[name]={ 
            old:parseInt(old)
      };
      return obj1;
  })();
  console.log(obj);
}
<input type="text" id="nacionality">
<input type="text" id="name">
<input type="text" id="old">
<button onclick="add()">Add</button>

Comments

1

Your JSON code is wrong, it should be something like:

{
    "Brazilians":[
        John:{ old: 18 },
        Jones:{ old: 20 }
    ],
    "Canadians":[
        Friedman:{ old: 22 },
        James:{ old: 35 }
    ]
}

Comments

0

to make dynamical prop in json you [prop]

function dynmObj (a, b){
    var json ={
      [a] : b
    } 
    return json;
}

1 Comment

Only not supported on IE11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.