tidwall / gjson

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

Unable to parse the json #227

Open manurawat opened 3 years ago

manurawat commented 3 years ago

I am trying to parse below json. Objective is to get loop across and get below result

Node: master1.prod.io step: 1 executionstate: SUCEEDED or NOT_STARTED

Node: master1.prod.io step: 2 executionstate: SUCEEDED or NOT_STARTED

Node: master1.prod.io step: 3 executionstate: SUCEEDED or NOT_STARTED

Node: etcd1.prod.io step: 1 executionstate: SUCEEDED or NOT_STARTED

Node: etcd1.prod.io step: 2 executionstate: SUCEEDED or NOT_STARTED

{"executionId":751456,"serverNode":"rundeck.io","nodes":{"master1.prod.io":[{"executionState":"NOT_STARTED","stepctx":"1"},{"executionState":"NOT_STARTED","stepctx":"2"},{"executionState":"NOT_STARTED","stepctx":"3"}],"etcd1.prod.io":[{"executionState":"SUCCEEDED","stepctx":"1"},{"executionState":"SUCCEEDED","stepctx":"2"},{"executionState":"SUCCEEDED","stepctx":"3"}]}}

I am able to parse if the node name is just master1 and not "master1.prod.io". I think it's not able to recognize dot i.e. "."

cjhouser commented 3 years ago

@manurawat Check out the escaping syntax shown here. Note that a quoted path uses different escaping than a path surrounded with back ticks. Both of the following should work:

gjson.Get(json, "nodes.master1\\.prod\\.io") // Quotes + double backslash
gjson.Get(json, `nodes.master1\.prod\.io`)   // Ticks + single backslash

Does that help?