check if the value of one json field is true based on yaml
Question:
This is my yaml
:
-
dedicatedip: 1.1.1.1
status: active
type: typeA
-
dedicatedip: 2.2.2.2
status: active
type: typeB
-
dedicatedip: 2.2.2.2
status: active
type: typeA
-
This is json
:
{
"error": false,
"data": {
"licenses": [
{
"id": 167689,
"type": "typeA",
"ip": "1.1.1.1",
"status": "Active",
"automated": false,
"notes": ""
},
{
"id": 167689,
"type": "typeA",
"ip": "2.2.2.2",
"status": "Active",
"automated": false,
"notes": ""
},
{
"id": 167689,
"type": "typeB",
"ip": "2.2.2.2",
"status": "Active",
"automated": true,
"notes": ""
}
]
}
}
I’m trying to find if the IP in yaml
has the same type in the json
(one IP may have different types, and each type is going to be checked separately), and then check the automated
field in the json
.
Expected output is:
IP 2.2.2.2, type typeA, automated is false.
IP 1.1.1.1, type typeA, automated is false.
This is my failed attempt so far:
import json, yaml
with open ("file.yaml", "r", encoding="utf-8") as yf, open("file.json", "r", encoding="utf-8") as jf:
ydata = list(yaml.safe_load_all(yf))
jdata = json.load(jf)
delta = [
(yd["dedicatedip"], jd["type"], yd["status"], jd["status"])
for yd in ydata[0] for jd in jdata["data"]["licenses"]
if yd and yd["dedicatedip"] == jd["ip"]
and yd["type"].replace("Type ", "").lower() == jd["type"].lower()
and jd["automated"] != 'true'
]
for vars in delta:
print('IP {}, license type {} automate is false in json.n'.format(*vars))
I get this output when I run, which is not correct:
IP 1.1.1.1, license type typeA, automate is false is json.
IP 2.2.2.2, license type typeB, automate is false is json.
IP 2.2.2.2, license type typeA, automate is false is json.
Answers:
jd["automated"]
is a bool not a string, so you should be using and not jd["automated"]
instead of and jd["automated"] != 'true'
in the condition.
This is my yaml
:
-
dedicatedip: 1.1.1.1
status: active
type: typeA
-
dedicatedip: 2.2.2.2
status: active
type: typeB
-
dedicatedip: 2.2.2.2
status: active
type: typeA
-
This is json
:
{
"error": false,
"data": {
"licenses": [
{
"id": 167689,
"type": "typeA",
"ip": "1.1.1.1",
"status": "Active",
"automated": false,
"notes": ""
},
{
"id": 167689,
"type": "typeA",
"ip": "2.2.2.2",
"status": "Active",
"automated": false,
"notes": ""
},
{
"id": 167689,
"type": "typeB",
"ip": "2.2.2.2",
"status": "Active",
"automated": true,
"notes": ""
}
]
}
}
I’m trying to find if the IP in yaml
has the same type in the json
(one IP may have different types, and each type is going to be checked separately), and then check the automated
field in the json
.
Expected output is:
IP 2.2.2.2, type typeA, automated is false.
IP 1.1.1.1, type typeA, automated is false.
This is my failed attempt so far:
import json, yaml
with open ("file.yaml", "r", encoding="utf-8") as yf, open("file.json", "r", encoding="utf-8") as jf:
ydata = list(yaml.safe_load_all(yf))
jdata = json.load(jf)
delta = [
(yd["dedicatedip"], jd["type"], yd["status"], jd["status"])
for yd in ydata[0] for jd in jdata["data"]["licenses"]
if yd and yd["dedicatedip"] == jd["ip"]
and yd["type"].replace("Type ", "").lower() == jd["type"].lower()
and jd["automated"] != 'true'
]
for vars in delta:
print('IP {}, license type {} automate is false in json.n'.format(*vars))
I get this output when I run, which is not correct:
IP 1.1.1.1, license type typeA, automate is false is json.
IP 2.2.2.2, license type typeB, automate is false is json.
IP 2.2.2.2, license type typeA, automate is false is json.
jd["automated"]
is a bool not a string, so you should be using and not jd["automated"]
instead of and jd["automated"] != 'true'
in the condition.