0

I have 2 JSON files.

file 1

{
  object: {
    variable: true
  }
}

and file 2

{
  object2: {
    tag: "object.variable
  }
}

What I am stuck on is that I need to, in one javascript file, get the tag from file 2 and use it to reference the variable in file 1.

I have gotten both file 1 as a JSON and have gotten 'tag' from file 2 in my code. However I am entirely lost as to how I can go about using the string "object.variable" to get the value of object.variable from file 1.

PS (I tried using eval but had no luck, though I would like to avoid it at all costs if possible.)

2
  • Your json is invalid. You don't use a ; in a json object. Also you have " in your object2 but if you are accessing a key from object we wouldn't need that. How are you actually accessing it? Give us the actual code. Commented Jul 3, 2019 at 19:21
  • Apologies on the JSON format. Those are example files. The real ones I'm using work to get the String and the JSON itself. The only thing I have tried that even got close was eval(string) where string was object.variable. This failed as it said periods were not defined for eval(). @IMustBeSomeone Commented Jul 3, 2019 at 19:28

3 Answers 3

3

Did you mean something like this ?

var file1 = {
  object: {
    variable: true
  }
};

var file2 = {
  object2: {
    tag: "object.variable"
  }
};

var result =  file2.object2.tag.split('.').reduce((a, b) =>  a ? a[b] : null, file1);


console.log(result);

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

4 Comments

I have only used .reduce a limited number of times so apologies if this is a silly question, but are 'a' and 'b' "object" and "variable" respectively?
a is for the accumulator, which is file1(passed as second param for reduce function) and b is each item in the array of tagnames(seperated by a dot).
So if tag was something like "object.one.two.three.variable", it would still work given that path exists?
Don't FULLY understand how it works, but it works. Great answer and kudos for making it a one-liner!
1

This could be the possible solution :

var query = {
  object2: {
    tag: "data.object.variable"
  }
}


var data = {
  object: {
    variable: true
  }
}

console.log(eval(query.object2.tag));

2 Comments

This seems to work in the code snippet but when I run it in my file, I received "UnhandledPromiseRejectionWarning: ReferenceError: period is not defined". I had tried something near identical to this answer but couldn't get around that error.
Avoid eval if you can not be 110% certain of the source data. Instead look at the code supplied by @Vishnu
0

But if you want to reference to the same json, this is one method that you can:

jsn = {
    x : 2,
    y : ()=>(jsn.x*2)
}

This way, you can refference one varriable to another varriable in same json

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.