javascript json config file to read data from another json

I have a json config file that describes a json data, but I dont know how to use the config to then read the json data itself

-- json config file
{
    "CustomerColumnPOS": 0,
    "ActivityDateColumnPOS": 1,
    "ActivityTimeColumnPOS": 2,  
    "CustomerColumnNm": "CustomerNum",
    "ActivityDateColumnNm": "Activity_Date",
    "ActivityTimeColumnNm": "Activity_Time",                       	
}

Open in new window


-- json data file
var json = {"CustomerNum":"12345","Activity_Date":"1/24/16","Activity_Time":"06:00:00"};

Open in new window


now I have a function that will receive the json data file and use the config file to interpret that json file.
This is where I need the help

// this is to get my config file
    var config = JSON.parse(JSON.minify(fs.readFileSync(__dirname + '/Config.json', 'utf8')));

// this is to copy the json data into my function
    var FunctionData = PassedDataJson;


// pull the data using the config positional columns
	var CustName1 =  // I need to use CustomerColumnPOS in the config file to then pull
					// data value from the FunctionData that relates to the CustomerNum

	var ActDate1 =  // I need to use CustomerColumnPOS in the config file to then pull
					// data value from the FunctionData that relates to the Activity_Date
					
	var ActTime1 =  // I need to use CustomerColumnPOS in the config file to then pull
					// data value from the FunctionData that relates to the CustomerNum					

// pull the data using the config Name columns
	var CustName2 =  // I need to use CustomerColumnNm in the config file to then pull
					// data value from the FunctionData that relates to the CustomerNum

	var ActDate2 =  // I need to use CustomerColumnNm in the config file to then pull
					// data value from the FunctionData that relates to the Activity_Date
					
	var ActTime2 =  // I need to use CustomerColumnNm in the config file to then pull
					// data value from the FunctionData that relates to the Activity_Time	

Open in new window

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.

zc2Commented:
I am not sure how that FunctionData  is related to the "json" object...
Here is an example how to use the column names from the config files to extract data from the json structure you posted.
"CustNum:" + json[config.CustomerColumnNm] + 
"\nActDate:" + json[config.ActivityDateColumnNm]  + 
"\nActTime:" + json[config.ActivityTimeColumnNm];

Open in new window

Is that your wanted?
1
leakim971PluritechnicianCommented:
you can't do this :
      var CustName1 =  // I need to use CustomerColumnPOS in the config file to then pull
                              // data value from the FunctionData that relates to the CustomerNum

      var ActDate1 =  // I need to use CustomerColumnPOS in the config file to then pull
                              // data value from the FunctionData that relates to the Activity_Date
                              
      var ActTime1 =  // I need to use CustomerColumnPOS in the config file to then pull
                              // data value from the FunctionData that relates to the CustomerNum      

because "CustomerNum" is not in first position

it's ordered by key name
it would be possible if you had something like :
var json = ["12345", "1/24/16", "06:00:00"];

what the purpose to use config POS and Nm to get the same data ?
1
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
the PassedDataJson will look like
{"CustomerNum":"12345","Activity_Date":"1/24/16","Activity_Time":"06:00:00"}

The config json will look like
{
    "CustomerColumnPOS": 0,
    "ActivityDateColumnPOS": 1,
    "ActivityTimeColumnPOS": 2,  
    "CustomerColumnNm": "CustomerNum",
    "ActivityDateColumnNm": "Activity_Date",
    "ActivityTimeColumnNm": "Activity_Time",                             
}

so I have the following function:

function (context, PassedDataJson) {
..    
// 1st pull in the json for to config
     var config = JSON.parse(JSON.minify(fs.readFileSync(__dirname + '/Config.json', 'utf8')));

// 2nd I would copy the PassedDataJson that was passed to a json variable that I can control inside the function
     var FunctionData = PassedDataJson;

// now I want  a seperate variable to pull data out of a particular element of the FunctionData
// 1st I want to pull the value by using the postion in the FunctionData that the value should exists according to the config

// looking at CustName1
// this variable will need to = "12345"
// to pull that value from the FunctionData json
// using the config I know CustomerColumnPOS gives me the value "0"
// so I want to use that "0" to pull out the value from the FunctionData json
      var CustName1 =  ....

// looking at CustName2
// this variable will need to = "12345"
// to pull that value from the FunctionData json
// using the config I know CustomerColumnNm gives me the value
// so I want to use that 0 to pull out the value from the FunctionData json
      var CustName1 =  ....
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!

leakim971PluritechnicianCommented:
we got it, but you did not answered the main question
// looking at CustName1
// this variable will need to = "12345"
// looking at CustName2
// this variable will need to = "12345"

Open in new window

Why do you want to get the same thing two different ways and the first way, using POS (number), is a nonsense with the current json object data, yes a nonsense
1
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
for the      
var CustName1 =  // I need to use CustomerColumnPOS in the config file to then pull
                              // data value from the FunctionData that relates to the CustomerNum

can I read the config file CustomerColumnPOS which gives me the value 0
then find the name of that item from the data json
    in this case the name of the item would be CustomerNum
    now i know the name "CustomerNum" can I then pull
    the value from the data json where the name  is CustomerNum ?
0
leakim971PluritechnicianCommented:
can I read the config file CustomerColumnPOS which gives me the value 0
yes, this wya :
var zero = config.CustomerColumnPOS; // same as config["CustomerColumnPOS"]

Open in new window

then find the name of that item from the data json
no, check my screenshot, let me know if you see CustomerNum in POSition 0, the nonsense is here, you can do this using array, not with this json object
in this case the name of the item would be CustomerNum
    now i know the name "CustomerNum" can I then pull
    the value from the data json where the name  is CustomerNum ?
so no, no and no :)
1
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
the reason that I am loooking to a postional version and a named version is that in some cases

I have data from the json where the postion of where the value of lands is important, regardless of what the name inside the json was called for that particular postion
   so the 1st postion name could be CustomerNum today and CustomerNXXX tomorrow, but when I use the positon version
   I just grab the value from what ever is there

in other cases I have json where the name is always fixed, but the postion inside the json may change from being the 1st to the last postion.


So I am looking to create a process that I can handle both cases and have a flag in my congfig that will ultimately determine which method to grab data is actually being used.
0
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
is there a way to read the name of the json elements based on where they reside?

so is the json is {"CustomerNum":"12345","Activity_Date":"1/24/16","Activity_Time":"06:00:00"}

is there a way for me to pull the name?
    CustomerNum
0
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
could I do something like:

// push all the keys into an array
var jsonKeys = Object.keys(FunctionData);

// pull the data using the config positional columns
      var CustName1Key = jsonKeys [FunctionData.CustomerColumnPOS];
// at this point i think the value of CustName1Key would be CustomerNum

// now I think I got the name im not sure how to use it in the code to pull back the value for CustomerNum that
// resides in FunctionData json
      var CustName1 =  FunctionData. // somehow i need to have the CustName1Key interpeted so it can be used
0
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
would something like the following be possible?

// this would be for the postional version if at all possible

      var jsonKeys = Object.keys(FunctionData);      
      var CustName1 =  FunctionData[jsonKeys[FunctionData.CustomerColumnPOS]];

// this would be for the name version based on what zc2 wrote
// pull the data using the config Name columns
      var CustName2 =  FunctionData[config.CustomerColumnNm];
0
leakim971PluritechnicianCommented:
from what you post, you should use :
var CustName1Key = jsonKeys [config.CustomerColumnPOS];
var CustName1 =  FunctionData[CustName1Key];

you should use your dev console to check
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
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
Thanks Leakim971, its a life saver. Could you just confirm my last comment to see if that makes sense.

Thanks zc2 that got me on a path to resolve my issue
0
leakim971PluritechnicianCommented:
yeahn you both did the job, good new year's Eve to you guys !
0
leakim971PluritechnicianCommented:
here a console screenshot :
console
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.