tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.1k stars 846 forks source link

how to extract quoted names? #260

Closed tooptoop4 closed 2 years ago

tooptoop4 commented 2 years ago

how to extract values eventName and x-amz-request-id from below? i tried on https://gjson.dev/ but couldn't work out the syntax to handle Records part.

Expected values: ObjectCreated:Put r6

{"Type":"Notification","MessageId":"r1","TopicArn":"r2","Subject":"Amazon S3 Notification","Message":"{\"Records\":[{\"eventVersion\":\"2.1\",\"eventSource\":\"aws:s3\",\"awsRegion\":\"r3\",\"eventTime\":\"2022-01-10T10:33:10.900Z\",\"eventName\":\"ObjectCreated:Put\",\"userIdentity\":{\"principalId\":\"r4\"},\"requestParameters\":{\"sourceIPAddress\":\"r5\"},\"responseElements\":{\"x-amz-request-id\":\"r6\",\"x-amz-id-2\":\"/r7\"},\"s3\":{\"s3SchemaVersion\":\"1.0\",\"configurationId\":\"r8\",\"bucket\":{\"name\":\"r9\",\"ownerIdentity\":{\"principalId\":\"r10\"},\"arn\":\"r11\"},\"object\":{\"key\":\"jj\",\"size\":2,\"eTag\":\"r12\",\"versionId\":\"r13\",\"sequencer\":\"r14\"}}}]}","Timestamp":"r15","SignatureVersion":"1","Signature":"r16","SigningCertURL":"r17","UnsubscribeURL":"https://sns.r.amazonaws.com/?Action=Unsubscribe\u0026SubscriptionArn=redd"}

tidwall commented 2 years ago

I just added the ability to extract values from stringified json. Make sure to get the latest version of gjson v1.13.0.

Try these paths.

Message.@fromstr.Records.#.eventName
Message.@fromstr.Records.#.responseElements.x-amz-request-id