0

I have a JSON file that contains some data that I need. I would like to write a python program to read it and get the information. Any help?

Here is a sample of the JSON

var case_data = {
  "cases": {
    "1": {
      "amount": 1500.0, 
      "case_id": "1", 
      "case_name": "US v. Control Systems Specialist, Inc. and Darrold Richard Crites", 
      "country": "br", 
      "sector": "sector-defense"
    }, 
    "10": {
      "amount": 0.0, 
      "case_id": "10", 
      "case_name": "SEC v. Int'l Systems & Controls Corp.", 
      "country": "cl", 
      "sector": "sector-agriculture"
    }
  }, 
  "countries": {
    "ae": {
      "cases": [
        "191", 
        "192", 
        "193", 
        "282", 
        "332"
      ], 
      "sectors": {
        "sector-consulting": {
          "total": 1812113.33
        }, 
        "sector-energy": {
          "total": 6622147.0
        }, 
        "sector-infrastructure": {
          "total": 4694551.0
        }
      }, 
      "total": 13128811.33, 
      "tree": {
        "children": [
          {
            "children": [], 
            "classname": "sector-energy", 
            "data": {
              "$amount": 4550000.0, 
              "$area": 3
            }, 
            "id": "191", 
            "name": "Control Components Inc. et al."
          }, 
          {
            "children": [], 
            "classname": "sector-infrastructure", 
            "data": {
              "$amount": 140551.0, 
              "$area": 2
            }, 
            "id": "192", 
            "name": "Textron Inc."
          }, 
          {
            "children": [], 
            "classname": "sector-infrastructure", 
            "data": {
              "$amount": 4554000.0, 
              "$area": 3
            }, 
            "id": "193", 
            "name": "York International Corp."
          }, 
          {
            "children": [], 
            "classname": "sector-consulting", 
            "data": {
              "$amount": 1812113.33, 
              "$area": 3
            }, 
            "id": "282", 
            "name": "Aon Corporation"
          }, 
          {
            "children": [], 
            "classname": "sector-energy", 
            "data": {
              "$amount": 2072147.0, 
              "$area": 3
            }, 
            "id": "332", 
            "name": "Tyco Int\u2019l Ltd. et al."
          }
        ], 
        "data": {
          "$amount": 0, 
          "$area": 14
        }, 
        "id": "ae", 
        "name": "UAE"
      }
    }, 
    "ao": {
      "cases": [
        "5", 
        "9", 
        "207", 
        "208", 
        "209"
      ], 
      "sectors": {
        "sector-consulting": {
          "total": 12350000.0
        }, 
        "sector-energy": {
          "total": 18097043.0
        }, 
        "sector-telecom": {
          "total": 7080000.0
        }
      }, 
      "total": 37527043.0, 
      "tree": {
        "children": [
          {
            "children": [], 
            "classname": "sector-energy", 
            "data": {
              "$amount": 302043.0, 
              "$area": 2
            }, 
            "id": "5", 
            "name": "ABB Ltd. et al."
          }, 
          {
            "children": [], 
            "classname": "sector-energy", 
            "data": {
              "$amount": 16335000.0, 
              "$area": 6
            }, 
            "id": "9", 
            "name": "Baker Hughes Inc. et al."
          }, 
          {
            "children": [], 
            "classname": "sector-energy", 
            "data": {
              "$amount": 1460000.0, 
              "$area": 3
            }, 
            "id": "207", 
            "name": "GlobalSanteFe Corp."
          }, 
          {
            "children": [], 
            "classname": "sector-telecom", 
            "data": {
              "$amount": 7080000.0, 
              "$area": 3
            }, 
            "id": "208", 
            "name": "Alcatel-Lucent S.A. et al."
          }, 
          {
            "children": [], 
            "classname": "sector-consulting", 
            "data": {
              "$amount": 12350000.0, 
              "$area": 6
            }, 
            "id": "209", 
            "name": "Panalpina World Transport (Holding) Ltd. et al."
          }
        ], 
        "data": {
          "$amount": 0, 
          "$area": 20
        }, 
        "id": "ao", 
        "name": "Angola"
      }
    }, 
  };

I want to extract the number right after "sector-energy" within each country. Note that in this sample file, there are two countries 'ae' and 'ao'.

2
  • you've read this: docs.python.org/3.3/library/json.html ? Commented Dec 9, 2013 at 22:54
  • 1
    That's not JSON, that's a Javascript statement that sets a variable named case_data to an object. Commented Dec 9, 2013 at 23:09

2 Answers 2

3

If you actually have JSON. all you can to do is use the json module to decode it into a native dict, just as you'd use the JSON object in JavaScript to decode it into a native object. Compare this JS:

var case_data = JSON.parse(data);

… to the equivalent Python:

case_data = json.loads(data)

Once you've done that, there's no JSON to worry about anymore; it's just normal native objects, which you can access the same way you would any other combination of dicts and lists and strings and numbers. For example:

sector_energy = [country["sectors"]["sector-energy"] 
                 for country in case_data["countries"]]

However, what you've shown us isn't JSON at all; it's JavaScript source code, which assigns a complex object to a variable. You can't parse that as JSON, in any language, because it's not.

Of course the part of the source code between the = and the ; is not only valid JavaScript code, but also valid JSON. It's also valid Python and valid Ruby, for that matter. But if you want to parse this file and others like it, you will need to come up with the rules for what fragments represent JSON that you want to parse. Is each file just a single JS variable assignment? Or something different?

At any rate, it's almost always much better to actually use JSON for interchange between languages, instead of using something sort-of-JSON-like and hoping it works.

Sign up to request clarification or add additional context in comments.

Comments

0

Python already has a library for this

http://docs.python.org/3.3/library/json.html

1 Comment

This answer would be more useful if it included an example instead of just a link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.