/    Sign up×
Community /Pin to ProfileBookmark

Text to json format question

If I have an external file containing this kind of information:

[code]
var Employees = [ // text file contents
“Lname,Fname,Age,Sex,Ext”, // description line
“Alley,Gasoline,75,M,x1234”,
“Boop,Betty,24,F,x2345”,
“Cow,Clarabel,35,F,x3456”,
“Duck,Daffy,58,M,x4567”,
“Fudd,Elmer,64,M,x5678”
];
[/code]

I would like to transform this using JS into a json object with a format like this:

[code]
var jsonObj[“Lname,Fname”] = {‘Age’:’???’, ‘Sex’:’???’, ‘Ext’:’???’ }
[/code]

How would I set the ‘Age’, ‘Sex’ and ‘Ext’ information for each employee?
(This assumes I am creating the jsonObj correctly [???])

Or should the transform be something like this?

[code]
jsonObject = {‘Names’:”, {‘Age’:”, ‘Sex’:”, ‘Ext’:” } }
jsonObj = new jsonObject;

// followed by some assignment to each field like
// jsonObj{Names.Emp[0]+’,’+Emp[1], {
// ‘Age’:Emp[2],’Sex’:Emp[3],’Ext’:Emp[4] } }
// after each ‘Employee’ element has been split to an array ‘Emp’
[/code]

This json stuff intrigues me, but I’m having a tough time wrapping my head around it.
😮

Am I going in the right direction?
Is this possible using JS without retyping my initial database?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meJun 22.2011 — [CODE] var Employees = [ // text file contents
"Lname,Fname,Age,Sex,Ext", // description line
"Alley,Gasoline,75,M,x1234",
"Boop,Betty,24,F,x2345",
"Cow,Clarabel,35,F,x3456",
"Duck,Daffy,58,M,x4567",
"Fudd,Elmer,64,M,x5678"
];

var output={}; //the final object to hold employees by key
var cols=Employees[0].split(","); //gets col names
cols[0]+=","+cols[1]; //merges last and first name columns
cols.splice(1,1); //kills now-redundant Fname column

Employees.slice(1). //skips col headers
map(function(row, index){ //iterates array via [].map()
var cells=row.split(","); //grabs row's cols
cells[0]+=","+cells[1]; //merges last and first name columns
cells.splice(1,1); //kills now-redundant Fname column
var sub=output[cells[0]]={}; //empty result "row"
cells.slice(1).map(function(cell, colIndex){//iterates cells using [].map()
//strongly sets the rowObject's column's value using the current cell:
sub[ cols[ colIndex +1 ] ] = isFinite(cell) ? cell*1 : cell;
});//end cell map()
});;//end row map()


alert(
JSON.stringify(

output , null, "t"
)
);

[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorJun 22.2011 — Thank you. I'll study on this a while, but at least it's looking manageable! ?
Copy linkTweet thisAlerts:
@JMRKERauthorJun 22.2011 — He's baaack!!!

In trying to understand the code you posted, I made 2 modifications.

1. I displayed an alert of the 'output' keys ... seems to work fine

  • 2. I tried to display the part of the 'output' using output{keys}.Age ... No Go!


  • I'm sure it's something I don't understand, but could you look at my code and see what I am still misunderstanding?

    I highlighted the changes in [COLOR="Red"]RED[/COLOR].

    If you uncomment the last of the testKeys function /* */ it breaks.

    With that section commented out, it works as I expect.

    <i>
    </i>[COLOR="Red"]&lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Array to JSON&lt;/title&gt;
    [/COLOR]
    &lt;script type="text/javascript"&gt;
    var Employees = [ // text file contents
    "Lname,Fname,Age,Sex,Ext", // description line
    "Alley,Gasoline,75,M,x1234",
    "Boop,Betty,24,F,x2345",
    "Cow,Clarabel,35,F,x3456",
    "Duck,Daffy,58,M,x4567",
    "Fudd,Elmer,64,M,x5678"
    ];

    var output={}; //the final object to hold employees by key

    var cols=Employees[0].split(","); //gets col names
    cols[0]+=","+cols[1]; //merges last and first name columns
    cols.splice(1,1); //kills now-redundant Fname column

    Employees.slice(1). //skips col headers
    map(function(row, index){ //iterates array via [].map()
    var cells=row.split(","); //grabs row's cols
    cells[0]+=","+cells[1]; //merges last and first name columns
    cells.splice(1,1); //kills now-redundant Fname column
    var sub=output[cells[0]]={}; //empty result "row"

    <i> </i>cells.slice(1).map(function(cell, colIndex){ //iterates cells using [].map()
    <i> </i> //strongly sets the rowObject's column's value using the current cell:
    <i> </i> sub[ cols[ colIndex +1 ] ] = isFinite(cell) ? cell*1 : cell;
    <i> </i>});//end cell map()

    });;//end row map()

    alert( JSON.stringify( output , null, "t" ) );
    [COLOR="Red"]
    function testKeys() {
    var str = 'Keysn'; for (var i in output) { str += i+'n'; }
    alert(str);

    <i> </i>str = 'Recsn';
    /*
    for (var i in output) {
    str += i+'t'+output{i}.Age; // +'t'+output{i}.Sex+'t'+output{i}.Ext;
    }
    */
    alert(str);
    }

    &lt;/script&gt;

    &lt;/head&gt;
    &lt;body&gt;
    &lt;button onclick="testKeys()"&gt;Keys &amp; Recs&lt;/button&gt;
    &lt;/body&gt;
    &lt;/html&gt;[/COLOR]
    Copy linkTweet thisAlerts:
    @rnd_meJun 23.2011 — [CODE] var Employees = [ // text file contents
    "Lname,Fname,Age,Sex,Ext", // description line
    "Alley,Gasoline,75,M,x1234",
    "Boop,Betty,24,F,x2345",
    "Cow,Clarabel,35,F,x3456",
    "Duck,Daffy,58,M,x4567",
    "Fudd,Elmer,64,M,x5678"
    ];

    var output={}; //the final object to hold employees by key

    var cols=Employees[0].split(","); //gets col names
    cols[0]+=","+cols[1]; //merges last and first name columns
    cols.splice(1,1); //kills now-redundant Fname column

    Employees.slice(1). //skips col headers
    map(function(row, index){ //iterates array via [].map()
    var cells=row.split(","); //grabs row's cols
    cells[0]+=","+cells[1]; //merges last and first name columns
    cells.splice(1,1); //kills now-redundant Fname column
    var sub=output[cells[0]]={}; //empty result "row"

    cells.slice(1).map(function(cell, colIndex){ //iterates cells using [].map()
    //strongly sets the rowObject's column's value using the current cell:
    sub[ cols[ colIndex +1 ] ] = isFinite(cell) ? cell*1 : cell;
    });//end cell map()

    });;//end row map()

    // alert( JSON.stringify( output , null, "t" ) );

    function testKeys() {
    var str = 'Keysn'; for (var i in output) { str += i+'n'; }
    alert(str);

    str = 'Recsn';

    for (var i in output) {
    str += i+'t'+output[i].Age+'t'+output[i].Sex+'t'+output[i].Ext+'n';
    }

    alert(str);
    }

    testKeys()[/CODE]
    Copy linkTweet thisAlerts:
    @JMRKERauthorJun 23.2011 — I see the { error } of my [ ways ] ! ?

    Thank you again!
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...