0

I need to access and conduct a series of operations on a big json object. Some things I need to read and alter are very deep into the tree with paths such as:

result.project.properties[0]['hudson.model.ParametersDefinitionProperty'][0].parameterDefinitions[0]['hudson.model.BooleanParameterDefinition']

This path is something that I will reference over and over. I'd like to be able to do something like:

key = "project.properties[0]['hudson.model.ParametersDefinitionProperty']  [0].parameterDefinitions[0]['hudson.model.BooleanParameterDefinition']"

so I can then read and or write to the path like this:

result[key]

but node doesn't even work with:

result['project.properties']

much less the entire deep path I have to use.

Is there a good way to make the path reusable without having to type the whole thing out more than once?

kcgolden
  • 430
  • 3
  • 13

2 Answers2

2

result[key] refers to an object (key), which you initialized as:

key = "project.properties[0]['hudson.model.ParametersDefinitionProperty']  [0].parameterDefinitions[0]['hudson.model.BooleanParameterDefinition']"

In your JSON object, there is no object with that large name, so it doesn't work. If you want to not type out that whole thing, try this:

shortResult = result.project.properties[0]['hudson.model.ParametersDefinitionProperty'][0].parameterDefinitions[0]['hudson.model.BooleanParameterDefinition'];

From here you can access things which are inside "BooleanParameterDefinition".

Aditya R.
  • 71
  • 1
  • 3
  • right, but not by reference. "shortResult" would just be a copy of BooleanParameterDefinition so changes to it would only apply to that copy, not the original json object. So I would essentially have to type out that entire json object address again to set it equal to shortResult. This is what I'm hoping to avoid. – kcgolden Mar 27 '13 at 16:51
  • I was wrong about this. shortResult does turn out to be a reference to that point in the object so typing "shortResult = ''" is the same as typing out the entire path and setting it equal to something, so your answer works. I actually wrote out this solution before I asked the question but just made an assumption that it wouldn't work. Sorry about that. – kcgolden Mar 27 '13 at 19:11
1

Have you tried something like

var reference = result.project.properties[0]['hudson.model.ParametersDefinitionProperty'][0].parameterDefinitions[0]['hudson.model.BooleanParameterDefinition'];

This will keep a reference to that 'location' in your larger object, and you can reference attributes inside is as reference[attribute].

The reason result['project.properties'] doesn't work is that it is looking for an attribute with the key 'project.properties', which your object doesn't have.

Nick Mitchinson
  • 5,452
  • 1
  • 25
  • 31
  • The only problem is that it's not a reference, but a copy of that location. If I want to alter items in the bigger json object, I have to type out that long address each time. – kcgolden Mar 27 '13 at 16:53
  • I do not believe that to be correct. Unless I'm wrong (which is possible cause I havn't been awake for that long) javascript saves object by reference unless you explicitly do a deep copy of it. – Nick Mitchinson Mar 27 '13 at 16:55
  • None-the-less, you can easily test this for yourself by make an object, referencing it, making a change to one, and logging the other. – Nick Mitchinson Mar 27 '13 at 16:56
  • I'll check and let you know. You could be right but I'm pretty sure node doesn't pass by reference. – kcgolden Mar 27 '13 at 17:04
  • I have tried in Chrome's explorer and confirmed the pass by reference. Here is a small gist of what I did. https://gist.github.com/nrmitchi/5256118 – Nick Mitchinson Mar 27 '13 at 17:10
  • Yes, you were right. I overthought this and inserted assumptions that I shouldn't have. I actually came up with this exact solution before I even asked the question but deleted it assuming it wouldn't work. – kcgolden Mar 27 '13 at 20:11