Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access JSON property with "-" dash

People also ask

Are dashes allowed in JSON?

Hyphens no longer allowed but JSON API recommends them by default #206.

How do I access JSON properties?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.


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"]

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 will give you the 'undefined' error.

This is the correct way

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

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"]

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 }}"