1

I was trying to parse a json file and select some attribute.

here is my code

import json
import urllib

results = json.load(urllib.urlopen("https://www.kimonolabs.com/api/adgmajn2?apikey=L63EvSC1x5vG8iSbm9Jon3784mkDp1Or"))

parse_result  = json.loads(results)
print parse_result 

And here is json data format

{
  "name": "google_test",
  "count": 20,
  "frequency": "Manual Crawl",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "thisversionrun": "Sun Jun 07 2015 17:19:33 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "content": "Parse handles everything you need to store data securely and efficiently in the cloud. Store basic data types, including locations and photos, and query across ...",
        "title": {
          "href": "https://parse.com/products/core",
          "text": "Parse Core | Everything your app needs to save data, be ..."
        },
        "index": 1,
        "url": "https://www.google.com.tw/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#/es_th=1&q=pase%20data"
      },
      {
        "content": "Parsing or syntactic analysis is the process of analysing a string of symbols, either ... In order to parse natural language data, researchers must first agree on the  ...",
        "title": {
          "href": "http://en.wikipedia.org/wiki/Parsing",
          "text": "Parsing - Wikipedia, the free encyclopedia"
        },
        "index": 2,
        "url": "https://www.google.com.tw/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#/es_th=1&q=pase%20data"
      },
      {
        "content": "In a data flow, Integration Services sources do the work of extracting data, parsing string data, and converting data to an Integration Services data type.",
        "title": {
          "href": "https://msdn.microsoft.com/en-us/ms141022.aspx",
          "text": "Parsing Data - MSDN - Microsoft"
        }

        ......
    ]
  }

However, it show me some error. I can't understand what is it.

Traceback (most recent call last): File "get_json_data_2.py", line 6, in parse_result = json.loads(results) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/init.py", line 338, in loads return _default_decoder.decode(s) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer

1
  • as long as your link is correct then json.load(" ") should return a python dict and you can parse through it using key values. Commented Jun 7, 2015 at 17:53

2 Answers 2

8

results already contains the parsed JSON tree. You don't need the loads part:

import json
import urllib

results = json.load(urllib.urlopen("https://www.kimonolabs.com/api/adgmajn2?apikey=L63EvSC1x5vG8iSbm9Jon3784mkDp1Or"))

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

1 Comment

Thanks, you have been a great help! I got it.
-3

You could convert json to yaml using for example this: http://jsontoyaml.com/ . Yaml is python "version" of json.

import yaml

stram = open("test", "r")
print yaml.load(stram)

4 Comments

what the heck does: "Yaml is python version of json." mean? That's nonsense.
um, but why I need to convert to yaml ? Doesn't normal json type work in python? Actually, I need to pass json file from other script. So, I am afraid of the error of type.
the OP is asking about parsing JSON with the standard library's json module. Your answer makes no sense and doesn't answer the question.
yaml support isn't even built in... json is.

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.