Unable to parse JSON

Hello,

Can someone help me extract the values of rx and tx from the values where id = 0?

{
	"vnstatversion": "1.18",
	"jsonversion": "1",
	"interfaces": [
		{
			"id": "eth0",
			"nick": "eth0",
			"created": {
				"date": {
					"year": 2020,
					"month": 5,
					"day": 12
				}
			},
			"updated": {
				"date": {
					"year": 2020,
					"month": 7,
					"day": 4
				},
				"time": {
					"hour": 23,
					"minutes": 11
				}
			},
			"traffic": {
				"total": {
					"rx": 7192772615,
					"tx": 5947236849
				},
				"days": [
					{
						"id": 0,
						"date": {
							"year": 2020,
							"month": 7,
							"day": 4
						},
						"rx": 197786423,
						"tx": 441845912
					},
					{
						"id": 1,
						"date": {
							"year": 2020,
							"month": 7,
							"day": 3
						},
						"rx": 300725490,
						"tx": 292062885
					},
					{
						"id": 2,
						"date": {
							"year": 2020,
							"month": 7,
							"day": 2
						},
						"rx": 408867380,
						"tx": 69951002
					},
					{
						"id": 3,
						"date": {
							"year": 2020,
							"month": 7,
							"day": 1
						},
						"rx": 211189193,
						"tx": 136202977
					},
					{
						"id": 4,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 30
						},
						"rx": 210589917,
						"tx": 184092594
					},
					{
						"id": 5,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 29
						},
						"rx": 264319534,
						"tx": 127726471
					},
					{
						"id": 6,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 28
						},
						"rx": 49572699,
						"tx": 189411231
					},
					{
						"id": 7,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 27
						},
						"rx": 207396097,
						"tx": 136136573
					},
					{
						"id": 8,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 26
						},
						"rx": 27127008,
						"tx": 10446909
					},
					{
						"id": 9,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 25
						},
						"rx": 14954802,
						"tx": 22620815
					},
					{
						"id": 10,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 24
						},
						"rx": 219951430,
						"tx": 159388056
					},
					{
						"id": 11,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 23
						},
						"rx": 41576816,
						"tx": 9598527
					},
					{
						"id": 12,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 22
						},
						"rx": 49001366,
						"tx": 25253714
					},
					{
						"id": 13,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 21
						},
						"rx": 177241619,
						"tx": 247202633
					},
					{
						"id": 14,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 20
						},
						"rx": 63746206,
						"tx": 77840436
					},
					{
						"id": 15,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 19
						},
						"rx": 161194914,
						"tx": 98073676
					},
					{
						"id": 16,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 18
						},
						"rx": 99570329,
						"tx": 3048129
					},
					{
						"id": 17,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 17
						},
						"rx": 83773841,
						"tx": 126383516
					},
					{
						"id": 18,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 16
						},
						"rx": 181339408,
						"tx": 229442495
					},
					{
						"id": 19,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 15
						},
						"rx": 255594016,
						"tx": 173546478
					},
					{
						"id": 20,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 14
						},
						"rx": 180230115,
						"tx": 99678328
					},
					{
						"id": 21,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 13
						},
						"rx": 133193614,
						"tx": 184873263
					},
					{
						"id": 22,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 12
						},
						"rx": 149645027,
						"tx": 25238561
					},
					{
						"id": 23,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 11
						},
						"rx": 82411519,
						"tx": 194256891
					},
					{
						"id": 24,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 10
						},
						"rx": 272542231,
						"tx": 97864012
					},
					{
						"id": 25,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 9
						},
						"rx": 111608423,
						"tx": 74902705
					},
					{
						"id": 26,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 8
						},
						"rx": 137980615,
						"tx": 97964922
					},
					{
						"id": 27,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 7
						},
						"rx": 97428896,
						"tx": 73272781
					},
					{
						"id": 28,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 6
						},
						"rx": 46608660,
						"tx": 207525698
					},
					{
						"id": 29,
						"date": {
							"year": 2020,
							"month": 6,
							"day": 5
						},
						"rx": 182071951,
						"tx": 111105629
					}
				]
			}
		}
	]
}

I’ve tried this but can’t get it to work

{% set value_json = {"vnstatversion":"1.18","jsonversion":"1","interfaces":[{"id":"eth0","nick":"eth0","created":{"date":{"year":2020,"month":5,"day":12}},"updated":{"date":{"year":2020,"month":7,"day":4},"time":{"hour":23,"minutes":11}},"traffic":{"total":{"rx":7192772615,"tx":5947236849},"days":[{"id":0,"date":{"year":2020,"month":7,"day":4},"rx":197786423,"tx":441845912},{"id":1,"date":{"year":2020,"month":7,"day":3},"rx":300725490,"tx":292062885},{"id":2,"date":{"year":2020,"month":7,"day":2},"rx":408867380,"tx":69951002},{"id":3,"date":{"year":2020,"month":7,"day":1},"rx":211189193,"tx":136202977},{"id":4,"date":{"year":2020,"month":6,"day":30},"rx":210589917,"tx":184092594},{"id":5,"date":{"year":2020,"month":6,"day":29},"rx":264319534,"tx":127726471},{"id":6,"date":{"year":2020,"month":6,"day":28},"rx":49572699,"tx":189411231},{"id":7,"date":{"year":2020,"month":6,"day":27},"rx":207396097,"tx":136136573},{"id":8,"date":{"year":2020,"month":6,"day":26},"rx":27127008,"tx":10446909},{"id":9,"date":{"year":2020,"month":6,"day":25},"rx":14954802,"tx":22620815},{"id":10,"date":{"year":2020,"month":6,"day":24},"rx":219951430,"tx":159388056},{"id":11,"date":{"year":2020,"month":6,"day":23},"rx":41576816,"tx":9598527},{"id":12,"date":{"year":2020,"month":6,"day":22},"rx":49001366,"tx":25253714},{"id":13,"date":{"year":2020,"month":6,"day":21},"rx":177241619,"tx":247202633},{"id":14,"date":{"year":2020,"month":6,"day":20},"rx":63746206,"tx":77840436},{"id":15,"date":{"year":2020,"month":6,"day":19},"rx":161194914,"tx":98073676},{"id":16,"date":{"year":2020,"month":6,"day":18},"rx":99570329,"tx":3048129},{"id":17,"date":{"year":2020,"month":6,"day":17},"rx":83773841,"tx":126383516},{"id":18,"date":{"year":2020,"month":6,"day":16},"rx":181339408,"tx":229442495},{"id":19,"date":{"year":2020,"month":6,"day":15},"rx":255594016,"tx":173546478},{"id":20,"date":{"year":2020,"month":6,"day":14},"rx":180230115,"tx":99678328},{"id":21,"date":{"year":2020,"month":6,"day":13},"rx":133193614,"tx":184873263},{"id":22,"date":{"year":2020,"month":6,"day":12},"rx":149645027,"tx":25238561},{"id":23,"date":{"year":2020,"month":6,"day":11},"rx":82411519,"tx":194256891},{"id":24,"date":{"year":2020,"month":6,"day":10},"rx":272542231,"tx":97864012},{"id":25,"date":{"year":2020,"month":6,"day":9},"rx":111608423,"tx":74902705},{"id":26,"date":{"year":2020,"month":6,"day":8},"rx":137980615,"tx":97964922},{"id":27,"date":{"year":2020,"month":6,"day":7},"rx":97428896,"tx":73272781},{"id":28,"date":{"year":2020,"month":6,"day":6},"rx":46608660,"tx":207525698},{"id":29,"date":{"year":2020,"month":6,"day":5},"rx":182071951,"tx":111105629}]}}]} %}

{{ value_json["interfaces"][0]["traffic"]["days"][0] }}

The data is being extracted from a command line sensor.

Per JSONpathfinder.com, this should be the syntax

1 Like

It’s slightly important to note that this solution gets the values you want and is probably correct for your application, but it doesn’t precisely answer your question.

The solution finds the rx and tx values from the first-listed day of the first-listed interface. If you can be confident that eth0 will be either the first or only interface, and know that the tool generating the JSON will always list the days in order, then you’re good.

Anything over and above that would require a tool to loop through the interfaces and days lists to pull out the one you want. For example, if you have jq installed, this will give you the eth0 id=0 dictionary you’re after, with rx and tx as top-level keys (substitute command.sh with your actual command):

command.sh | jq '.interfaces[] | select(.id=="eth0")' | jq '.traffic.days[] | select(.id==0)'

That returns:

{
  "id": 0,
  "date": {
    "year": 2020,
    "month": 7,
    "day": 4
  },
  "rx": 197786423,
  "tx": 441845912
}
1 Like

Magnificent, thank you so much.