-1

Im sure this must have been asked before but I can't find an example on SO. I have a JSON string that starts out life as something like this:

{"model":"14","imgsize":"890","selection":{"SC":"BC","PC":"AC"},"changed":{"PC":"AC"}}

The string needs to be changed on user input such that "selection" records all the input the user has click on and "changed" is the last thing the user clicks on.

So I have a function that reads the JSON string from a textarea, modifies it dependant on what the user has selected (node and value) and then writes it back to the text area for debugging.

function changeJSON(node, value) {
    json = JSON.parse($('#json').val());
    json.selection[node] = value;
    delete json.changed;
    json.changed = {node:value};
    $('#json').val(JSON.stringify(json));
}

"selection" works nicely but "changed" updates to the literal variable name I pass it (in this case node) I.e. if I called the function with changeJSON("BC","HC") the JSON string becomes:

{"model":"14","imgsize":"890","selection":{"SC":"BC","PC":"AC","BC":"HC"},"changed":{"node":"HC"}}

I understand what javascript is trying to do but I want the changed element to be what my variable contains i.e.

,"changed":{"BC","HC"}

and not

,"changed":{"node","HC"}

I'd love someone to tell me what I am doing wrong!?

EDIT

Solved - see below for Quentin explanation as to why and my answer for the code changes necessary - hope it helps others.

11
  • 1
    Is that your real json? It is invalid. jsonlint.com Commented Apr 4, 2013 at 13:04
  • 2
    possible duplicate of how to assign variable value as variable name in a hash? Commented Apr 4, 2013 at 13:07
  • By storing your data as a JSON string, you incur the overhead of repeatedly parsing and stringifying your object every time you wish to change a property value. Why not just save it as an object, and stringify it when needed? Commented Apr 4, 2013 at 13:08
  • 1
    @SheikhHeera yes OK i failed to put the script on the end. Commented Apr 4, 2013 at 13:11
  • 1
    Ah I thought you want to find a solution for it, not (only) an explanation. The explanation is simple: Keys in object literals are interpreted literally, they are not resolved as identifiers. This is defined in the ECMAScript specification. Commented Apr 4, 2013 at 13:14

2 Answers 2

2

I don't think this is the same question, mine is why the literal variable name is used rather than the contents of the variable

The question referenced explains how to resolve the issue, but since you are asking for an explanation.

A variable name is a JavaScript identifier.

A property name in object literal syntax is also a JavaScript identifier (although you can use a string literal instead).

Since an identifier cannot be both a variable and a property name at the same time, you cannot use variables for property names in object literal syntax.

You have to, as described in the referenced question, create the object and then use the variable in square bracket notation.

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

1 Comment

Thanks you I just figured that myself and was coming back to answer the questions. Many thanks and shall give you the credit!
0

The solution, as Quentin suggested is to create th object first i.e.

delete json.changed;
json.changed = {};
json.changed[node] = value;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.