1

I am trying to create a nested object like this:

[{
   "Countrys":{
                "CountryCode":"AF",
                "CountryName":"AFGHANISTAN",
                "CreatedBy":"[email protected]",
                "CreatedOn":null,
                "ModifiedBy":"Admin",
                "ModifiedOn":"/Date(1394094559000)/"
              },
    "StateCode":"BAM",
    "CountryCode":"AF",
    "StateName":"BAMIAN",
    "CreatedBy":"[email protected]",
    "CreatedOn":"/Date(1372617000000)/",
    "ModifiedBy":null,
    "ModifiedOn":null
    },
    ...........
]

My code :

    updateStateList = [];
    //state countrys data variable
    var stateCountrysCountryCode;
    var stateCountrysCountryName;
    var stateCountrysCreatedBy;
    var stateCountrysCreatedOn;
    var stateCountrysModifiedBy;
    var stateCountrysModifiedOn;

    //state open data variable
    var StateCode;
    var CountryCode;
    var StateName;
    var CreatedBy;
    var CreatedOn;
    var ModifiedBy;
    var ModifiedOn;

    $(".tableRow").each(function () {

        stateCountrysCountryCode = $(this).find("#erSLCountrysCountryCode").val();
        stateCountrysCountryName = $(this).find("#erSLCountrysCountryName").val();
        stateCountrysCreatedBy = $(this).find("#erSLCountrysCreatedBy").val();
        stateCountrysCreatedOn = $(this).find("#erSLCountrysCreatedOn").val();
        stateCountrysModifiedBy = $(this).find("#erSLCountrysModifiedBy").val();
        stateCountrysModifiedOn = $(this).find("#erSLCountrysModifiedOn").val();

        StateCode =  $(this).find("#erSLStateCode").val();
        CountryCode = $(this).find("#erSLCountryCode").val();
        StateName = $(this).find("#erSLStateName").val();
        CreatedBy = $(this).find("#erSLCreatedBy").val();
        CreatedOn = $(this).find("#erSLCreatedOn").val();
        ModifiedBy = $(this).find("#erSLModifiedBy").val();
        ModifiedOn = $(this).find("#erSLModifiedOn").val();

        CountrysObj = {};
        CountrysObj["CountryCode"] = stateCountrysCountryCode;
        CountrysObj["CountryName"] = stateCountrysCountryName;
        CountrysObj["CreatedBy"] = stateCountrysCreatedBy;
        CountrysObj["CreatedOn"] = stateCountrysCreatedOn;
        CountrysObj["ModifiedBy"] = stateCountrysModifiedBy;
        CountrysObj["ModifiedOn"] = stateCountrysModifiedOn;

        //state open data
        statesObj = {};
        statesObj["StateCode"] = StateCode;
        statesObj["CountryCode"] = CountryCode;
        statesObj["StateName"] = StateName;
        statesObj["CreatedBy"] = CreatedBy;
        statesObj["CreatedOn"] = CreatedOn;
        statesObj["ModifiedBy"] = ModifiedBy;
        statesObj["ModifiedOn"] = ModifiedOn;




        //CountrysObj.push(statesObj["StateCode"]);

        updateStateList.push({ "Countrys": CountrysObj });
        updateStateList.push(statesObj);

    });



    alert(JSON.stringify(updateStateList));

I am getting like this Json:

[{"Countrys":{"CountryCode":"AX","CountryName":"ALAND ISLANDS","CreatedBy":"[email protected]","CreatedOn":"","ModifiedBy":"Admin","ModifiedOn":"/Date(1394094559000)/"}},{"StateCode":"NS","CountryCode":"AX","StateName":"NOT SPECIFIED","CreatedBy":"[email protected]","CreatedOn":"/Date(1372617000000)/","ModifiedBy":"","ModifiedOn":""}]

In this result I get extra closing 2nd braces.

1
  • What is the question? You are not clear enough Commented May 2, 2016 at 7:08

1 Answer 1

1

In Javascript, doing:

x = {}

creates an object. That's why adding (pushing) it to an array creates the result of [{}]

What you want is to create an object of all state data, and have countries as a property of that object.

something like this:

//state open data
statesObj = {};
statesObj["StateCode"] = StateCode;
statesObj["CountryCode"] = CountryCode;
statesObj["StateName"] = StateName;
statesObj["CreatedBy"] = CreatedBy;
statesObj["CreatedOn"] = CreatedOn;
statesObj["ModifiedBy"] = ModifiedBy;
statesObj["ModifiedOn"] = ModifiedOn;
statesObj["Countries"] = CountrysObj;
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help @SoloThink. In StackOverflow it is customary to mark helpful posts by upvoting and/or marking helpful answers as the accepted answers.

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.