Python Forum
Boolean Logic in Json
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Boolean Logic in Json
#1
Hi all, I'm pretty new to python and I've this problem.

I've the following json:
{
  "condition": "AND",
  "rules": [
    {
      "condition": "OR",
      "rules": [
        {
          "id": "u.url",
          "operator": "contains",
          "value": "URL_A"
        },
        {
          "id": "u.url",
          "operator": "contains",
          "value": "URL_B"
        }
      ]
    },
    {
      "condition": "OR",
      "rules": [
        {
          "id": "u.url",
          "operator": "contains",
          "value": "URL_C"
        },
        {
          "id": "u.url",
          "operator": "contains",
          "value": "URL_D"
        }
      ]
    }
  ] }
representing the boolean formula: 
(A or B) and (C or D)

 where  A
, for example, is this JSON object:
{
   "id": "u.url",
   "operator": "contains",
   "value": "URL_A"
}
In general, the JSON file could represent any boolean logic with (only) AND and OR operators.
The problems is to return a string representing the Sum Of Product of the original formula. That is: 
(A or C) and (A or D) and (B or C) and (B or D)

For example the result would be:

(u.url contains URL_A) OR (u.url contains URL_C) AND
(u.url contains URL_A) OR (u.url contains URL_D) AND
(u.url contains URL_B) OR (u.url contains URL_C) AND
(u.url contains URL_B) OR (u.url contains URL_D)
I've no idea how to solve this problem, please can you help me?
Reply
#2
You most show what you have tried,
just pasting in a task is not popular in any programming forum. 

To help you get started,first read in json.
import json

with open('data.json') as j:
   json_data = json.load(j)
Output:
>>> json_data {'condition': 'AND',  'rules': [{'condition': 'OR',             'rules': [{'id': 'u.url',                        'operator': 'contains',                        'value': 'URL_A'},                       {'id': 'u.url',                        'operator': 'contains',                        'value': 'URL_B'}]},            {'condition': 'OR',             'rules': [{'id': 'u.url',                        'operator': 'contains',                        'value': 'URL_C'},                       {'id': 'u.url',                        'operator': 'contains',                        'value': 'URL_D'}]}]}
It's now a python dictionary.
To navigate remember there also lists in this dictionary.
>>> json_data["condition"]
'AND'
>>> # with list [0] to navigate
>>> json_data["rules"][0]['rules'][0]
{'id': 'u.url', 'operator': 'contains', 'value': 'URL_A'}
>>> json_data["rules"][0]['rules'][1]
{'id': 'u.url', 'operator': 'contains', 'value': 'URL_B'}
>>> json_data["rules"][0]['rules'][1]['value']
'URL_B'
Reply
#3
I know I most show what I have tried, but I'm very no idea how to solve this  Wall
Could you help me?
Reply
#4
(Dec-20-2016, 06:12 PM)trinity_np Wrote: I know I most show what I have tried...

Well, you didn't show anything.  Does that mean you've tried nothing?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020