186

I am unable to retrieve a value from a json object when the string has a dash character:

{
"profile-id":1234, "user_id":6789
}

If I try to reference the parsed jsonObj.profile-id it returns ReferenceError: "id" is not defined but jsonObj.user_id will return 6789

I don't have a way to modify the values being returned by the external api call and trying to parse the returned string in order to remove dashes will ruin urls, etc., that are passed as well. Help?

user1902467
  • 1,873
  • 2
  • 12
  • 4
  • 4
    It might help to add information on what language/parser you are trying to use to parse the JSON. – Mike Brant Dec 13 '12 at 22:30
  • 1
    `Utilities.jsonParse` doesn't say much. – Darin Dimitrov Dec 13 '12 at 22:31
  • 1
    Does this answer your question? [How do I reference a javascript object property with a hyphen in it?](https://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) – Henke Jan 29 '21 at 10:30

5 Answers5

376

jsonObj.profile-id is a subtraction expression (i.e. jsonObj.profile - id).

To access a key that contains characters that cannot appear in an identifier, use brackets:

jsonObj["profile-id"]
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    Thanks! For information : It also work with angular : {{ jsonObj.attributes["profile-id"] }} – BastienSander May 13 '14 at 10:32
  • 3
    when this works in javascript and its the native implementation to access an object property key by string, sure it works in everything on top – john Smith Jan 12 '16 at 22:15
  • 1
    Also works for JS "style" object! @SLaks I extrapolated this solution to using the JavaScript style object for setting "box-shadow" property. `document.getElementById("someId").style["box-shadow"]="2px 2px 2px #616161";` works great! – Eric Hepperle - CodeSlayer2010 Oct 05 '16 at 16:15
  • 3
    @EricHepperle-CodeSlayer2010: You should use `style.boxShadow` instead. The `style` object converts hyphens to camelCase. – SLaks Oct 05 '16 at 16:21
  • Sweet! Thanks... I like camel case,method better because less characters to type. Verified it works!! – Eric Hepperle - CodeSlayer2010 Oct 05 '16 at 16:25
6

In addition to this answer, note that in Node.js if you access JSON with the array syntax [] all nested JSON keys should follow that syntax

This is the wrong way

json.first.second.third['comment']

And will give you the 'undefined' error.

This is the correct way

json['first']['second']['third']['comment'] 
Ahmad Adibzad
  • 501
  • 2
  • 6
  • 14
1

For ansible, and using hyphen, this worked for me:

    - name: free-ud-ssd-space-in-percent
      debug:
        var: clusterInfo.json.content["free-ud-ssd-space-in-percent"]
t.vdh
  • 19
  • 3
0

For anyone trying to apply the accepted solution to HomeAssistant value templates, you must use single quotes if you are nesting in doubles:

value_template: "{{ value_json['internet-computer'].usd }}"
brianfit
  • 1,829
  • 1
  • 19
  • 34
0

If you are in Linux, try using the following template to print JSON value which contains dashes '-'

jq '.["value-with-dash"]'

It worked for me.

James Risner
  • 5,451
  • 11
  • 25
  • 47