Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMESPath - parse raw string as object

Tags:

jmespath

I have an object that I am parsing using JMESPath where the value of a property is a JSON object encoded as a string. I want to be able to parse that string property as a JSON Object and work with it using JMESPath only (I'm aware I could parse the value using a JSON encoder).

Here is the object:

{
    "ARN": "arn:aws:secretsmanager:us-east-1:xxxxx:secret:todobackend/db/credentials-AP57Kn",
    "Name": "todobackend/db/credentials",
    "VersionId": "c95fae54-e7b4-4c7f-80d6-2c5649f86570",
    "SecretString": "{\"MYSQL_USER\":\"todobackend\",\"MYSQL_PASSWORD\":\"password\"}",
    "VersionStages": [
        "AWSCURRENT"
    ],
    "CreatedDate": 1523276277.046
}

So I want to parse the SecretString property as a JSON object.

Any ideas on whether or not this is possible?

like image 522
mixja Avatar asked Apr 14 '18 04:04

mixja


People also ask

How do I parse a string in JSON?

Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

Is JQ a JMESPath?

jq is typically used for the former, JMESPath for the latter. There's no reason why the remote service couldn't accept a jq filter, or that you couldn't use a JMESPath-based executable.

What does JMESPath stand for?

JMESPath (JSON Matching Expression paths) is a query language for search JSON documents.


1 Answers

jq will save you:

aws secretsmanager get-secret-value --secret-id todobackend/db/credentials-AP57K | jq -r '.SecretString|fromjson|.MYSQL_USER,.MYSQL_PASSWORD'
like image 66
Mike Patnode Avatar answered Oct 17 '22 05:10

Mike Patnode