Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON - Access field named '*' asterisk

I am trying to access a JSON field that has the key '*':

{ 
  "parse": { 
    "text": {
      "*": "text i want to access" 
    }
  }
}

Neither myObject.parse.text.* nor myObject.parse.text[0] works.

I have searched for an hour but haven't found any hint that an asterisk has special meaning. If I just traverse the complete tree and make String comparison with if (key == "*") I can retrieve the text, but I would like to access this field directly. How can I do that?

like image 236
Peter Avatar asked Sep 30 '11 12:09

Peter


People also ask

What does asterisk mean in JSON?

Using wildcards in resource ARNs An asterisk (*) represents any combination of characters and a question mark (?) represents any single character.

Can JSON have special characters?

In JSON the only characters you must escape are \, ", and control codes. Thus in order to escape your structure, you'll need a JSON specific function.

How do you handle an apostrophe in JSON?

Insert JSON data into a table: To use an apostrophe or a single quotation mark (') in a value, add another single quotation mark after the first one.


3 Answers

json.parse.text["*"]

Yucky name for an object member.


Asterisks have no special meaning; it's a string like any other.

myObject.parse.text.* doesn't work because * isn't a legal JS identifier. Dot notation requires legal identifiers for each segment.

myObject.parse.text[0] doesn't work because [n] accesses the element keyed by n or an array element at index n. There is no array in the JSON, and there is nothing with a 0 key.

There is an element at the key '*', so json.parse.text['*'] works.

like image 56
Dave Newton Avatar answered Sep 20 '22 07:09

Dave Newton


Try use the index operator on parse.text:

var value = object.parse.text["*"];
like image 29
Fox32 Avatar answered Sep 21 '22 07:09

Fox32


try to use

var text = myObject.parse.text['*']
like image 41
genesis Avatar answered Sep 22 '22 07:09

genesis