0

how do i populate these following JSON data on a html input field on the screen ? this is the json arraylist

[{
    "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check",}]

i have stored this above data in a JSON file how do i get it on the screen?

<input type="text" id="BankAccountName" />
<input type="text" id="CurrencyCode" />
<input type="text" id="DepositDate" />
<input type="text" id="PaymentChannel" />
1

6 Answers 6

1

This is a very basic way of populating input fields with the relevant values

var jsonData = [{
    "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check",}]

$("#BankAccountName").val(jsonData[0]['Bank Account Name']);
$("#CurrencyCode").val(jsonData[0]['Currency Code']);
$("#DepositDate").val(jsonData[0]['Deposit Date']);
$("#PaymentChannel").val(jsonData[0]['Payment Channel'])

DEMO

Note: You have not referred how this json is loading. Is it in an external file or it is supplied through an api.

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

Comments

0

Another approach:

var json = [{
        "Bank Account Name": "State Bank",
        "Currency Code": "4000",
        "Deposit Date": "5/2/1794",
        "Payment Channel": "check",}]

Object.keys(json[0]).map(value => {
  document.getElementById(value.replace(/ /g, "")).value = json[0][value]
})

The benefits are you won't need to change anything if you will add some new fields to JSON.

Comments

0

You can try below code:

var data = [{
  "Bank Account Name": "State Bank",
  "Currency Code": "4000",
  "Deposit Date": "5/2/1794",
  "Payment Channel": "check",
}];

$(document).ready(function() {
  var jsonObj = data[0];
  for (var key in jsonObj) {
    if (jsonObj.hasOwnProperty(key)) {

      $("#" + key.replace(/ /g, "")).val(jsonObj[key]);

    }
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="text" id="BankAccountName" />
<input type="text" id="CurrencyCode" />
<input type="text" id="DepositDate" />
<input type="text" id="PaymentChannel" />

Comments

0

var myvariable = [{"id":"15aea3fa","firstname":"John","lastname":"Doe"}];
$('#mything').text('id:'+myvariable[0].id+' name:'+myvariable[0].firstname+' '+myvariable[0].lastname);

var value=[{
    "BankAccountName": "State Bank",
    "CurrencyCode": "4000",
    "DepositDate": "5/2/1794",
    "PaymentChannel": "check",}];
$('#BankAccountName').val(value[0].BankAccountName);
$('#CurrencyCode').val(value[0].CurrencyCode);
$('#DepositDate').val(value[0].DepositDate);
$('#PaymentChannel').val(value[0].PaymentChannel);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
processed:<div id='mything'></div>
<input type="text" id="BankAccountName" />
<input type="text" id="CurrencyCode" />
<input type="text" id="DepositDate" />
<input type="text" id="PaymentChannel" />

Comments

0

there you go, a quick jquery solution.

var json = [{
    "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}];

$.each(json[0] , function (key, value){
    key = key.replace(/ /g,'');
    $('input[id='+key+']').val(value);
});

Comments

0

You can iterate through json data and set the value of text field. `

 var data= [{
    "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check",}];

$.each(data[0],function(key,value){
id=key.replace(" ","");


document.getElementById(id).value=value;
});

Comments

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.