help using javascript to pull out data from a json file

I have a json row that i am trying to process example below
{"CustomerNum":"12345","Activity_Date":"1/24/16"}

so assume this data is passed to my function, I need to pull out the customer number and split the activity date into three other attributes.


function (context, myDataQueRow) {    
..
..
   // get a copy of the data
    var ActvityData = myDataQue;
       
    //looking to place the values of this data into other variables
      var CustNum =  // get the value from Customer Number
      
      // the date is pulled from Activity_date, and has a format of MM / DD / YY
      var ActivityMonth  = // Activity_date pull from the 1st char up to the 1st /
      var ActivityDay  = // Activity_date pull from the 1st / up to the second /
      var ActivityYear = // Activity_date pull from the second / up to the end, also prefix with 20 since need 4 digit year
..
..
..
LVL 2
Jayesh AcharyaTechnichal ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
var data={"CustomerNum":"12345","Activity_Date":"1/24/16"};
function myFnc(arg){ 
  var b=arg.Activity_Date;
  b=b.split("/");
  return b; 
}
console.log(myFnc(data));

Open in new window

1
zephyr_hex (Megan)DeveloperCommented:
The example json you posted is already an object.  You can access the properties like so:

var json = {"CustomerNum":"12345","Activity_Date":"1/24/16"};

console.log(json.CustomerNum);
console.log(json.Activity_Date);  //this is a string, so you will need to create a JS date object in order to get the parts.
var d = new Date(json.Activity_Date);
console.log(d);
console.log(d.getMonth() + 1);
console.log(d.getDate());
console.log(d.getFullYear());

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zephyr_hex (Megan)DeveloperCommented:
@Leonidas:  It's better to use a Date object than parse the string.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Jayesh AcharyaTechnichal ConsultantAuthor Commented:
on the line
var d = new Date(json.Activity_Date);

// is there a way to specify the format of the date, in this case its MM/DD/YY, but in other cases I have DD/MON/YYYY e.g. 24/JAN/2016

also trying to understand this line
console.log(d.getMonth() + 1);

// what is the + 1 needed for?  if I would of read that in other languages I would of thought that would give me a value for February in this case
0
zephyr_hex (Megan)DeveloperCommented:
Regarding the format of the date:  If possible, it would be best to standardize your date format before passing in json, so that all of the Activity_Date results are in the same format.

If that is not possible, you will need to pass some kind of flag or indicator so that you know what format Activity_Date is in, and then parse the Activity_Date string accordingly to create a JS date object.  This is not the ideal method.

Regarding getMonth() + 1:
An integer number, between 0 and 11, representing the month in the given date according to local time. 0 corresponds to January, 1 to February, and so on

getMonth() is indexed starting at 0, so if you want to correlate the result with the months of the year, you need to add one.  Otherwise, you'll need to account for 0 being January.
1
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
Thank you both for the information, this is a bit of life saver for me trying to understand how this actually works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.