Thread: json parsing

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2015
    Posts
    35
    Rep Power
    4

    json parsing


    I am trying to parse the json ,but getting the following error

    Traceback (most recent call last):
    File "a11.py", line 4, in <module>
    data = json.load(data_file)
    File "/usr/lib/python2.7/json/__init__.py", line 291, in load
    **kw)
    File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    Code:
    test.py
    
    import json
    
    with open('/home/test/a.json') as data_file:
        data = json.load(data_file)
    
    for a in data['events']:
        a['customParameters'] = a['customParameters'][1]
    
    new_some_json_string = json.dumps(data, indent=2)
     
    print(new_some_json_string)
    
    
    data file :a.json 
    
     "events": [
    {
          "time": 1522224314,
          "customParameters": [
    {
              "group": "tag",
              "item": "else"
            },
    
            {
              "group": "url",
              "item": "https://www.xxx.a/3990154.html"
            },
            {
              "group": "title",
              "item": "a90189275"
            }
          ],
          "site": "xx877",
          "type": "test",
          "userId": "xx1"
        }
    ]
    
    expecting the output 
    site      type   userId   group  item            group1  item1
    xx877   test    xx1       url       https...        title      a90189275
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,951
    Rep Power
    509
    Great post---all the input, code, and expected output. Thank you.
    Something's wrong with the json, your program seems good. I looked up valid json examples on the good internet, then inserted outermost curly braces.
    Code:
    import io
    import json
    
    # print(json.dumps(data, indent=2)) pretty printed this json
    source = io.StringIO('''
    {
      "events": [
        {
          "time": 1522224314,
          "customParameters": [
            {
              "group": "tag",
              "item": "else"
            },
            {
              "group": "url",
              "item": "https://www.xxx.a/3990154.html"
            },
            {
              "group": "title",
              "item": "a90189275"
            }
          ],
          "site": "xx877",
          "type": "test",
          "userId": "xx1"
        }
      ]
    }
    ''')
    
    data = json.load(source)
    
    for a in data['events']:
        a['customParameters'] = a['customParameters'][1]
    
    new_some_json_string = json.dumps(data, indent=2)
     
    print(new_some_json_string)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2015
    Posts
    35
    Rep Power
    4
    Thanks for your quick reply.Title info missed in that out put.I am expecting group:title and group:url to be merge with 1 line and respective values

    ex:
    "group": "url"||"title"
    "item": "https://www.xxx.a/3990154.html"||"a90189275"

    Code:
    output of the code
    
    {
      "events": [
        {
          "customParameters": {
            "group": "url",
            "item": "https://www.xxx.a/3990154.html"
          },
          "time": 1522224314,
          "userId": "xx1",
          "site": "xx877",
          "type": "test"
        }
      ]
    }
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,951
    Rep Power
    509
    Oh you really meant it. Perhaps you can use the function with the long name.
    Code:
    import io
    import json
    import collections
    
    def recursively_search_a_collection_for_dictionary_value_having_a_particular_key(top:collections.Collection, key:hash)->list:
        result = []
        devilish_detail(top, key, result)
        return result
    
    def devilish_detail(top:type, key: hash, result: list)->None:
        '''
            implemented for lists and dictionaries.
        '''
        if isinstance(top, (list, tuple)):
            for t in top:
                devilish_detail(t, key, result)
        elif isinstance(top, dict):
            if key in top:
                result.append(top[key])
            for t in top.values():
                devilish_detail(t, key, result)
    
    '''
    "group": "url"||"title"
    "item": "https://www.xxx.a/3990154.html"||"a90189275"
    '''
    
    # print(json.dumps(data, indent=2)) pretty printed this json
    source = io.StringIO('''
    {
      "events": [
        {
          "time": 1522224314,
          "customParameters": [
            {
              "group": "tag",
              "item": "else"
            },
            {
              "group": "url",
              "item": "https://www.xxx.a/3990154.html"
            },
            {
              "group": "title",
              "item": "a90189275"
            }
          ],
          "site": "xx877",
          "type": "test",
          "userId": "xx1"
        }
      ]
    }
    ''')
    
    d = json.load(source)
    
    print(recursively_search_a_collection_for_dictionary_value_having_a_particular_key(d, 'group'))
    Last edited by b49P23TIvg; March 30th, 2018 at 03:47 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2015
    Posts
    35
    Rep Power
    4
    Thanks ,but getting the following error
    Code:
    Traceback (most recent call last):
      File "a1.py", line 5, in <module>
        def recursively_search_a_collection_for_dictionary_value_having_a_particular_key(top:collections.Collection, key:hash)->list:
    AttributeError: module 'collections' has no attribute 'Collection'
  10. #6
  11. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,951
    Rep Power
    509
    It runs in properly installed python3. Of course you can get rid of the annotations since they don't affect program execution.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2015
    Posts
    35
    Rep Power
    4
    I am running through python3,but getting the following error
    Code:
    python3 a1.py
    Traceback (most recent call last):
      File "a1.py", line 5, in <module>
        def recursively_search_a_collection_for_dictionary_value_having_a_particular_key(top:collections.Collection, key:hash)->list:
    AttributeError: module 'collections' has no attribute 'Collection'
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2015
    Posts
    35
    Rep Power
    4
    After removing Collections from the below function executed but no data
    Code:
    def recursively_search_a_collection_for_dictionary_value_having_a_particular_key(top:collections, key:hash)->list:
        result = []
        devilish_detail(top, key, result)
        return result
    
     python3 a1.py
    ['tag', 'url', 'title']
  16. #9
  17. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,951
    Rep Power
    509
    The key "group" appears three times in the json with values ['tag', 'url', 'title'] . You'll need to do some work to figure out why you don't want 'tag' in your output, to find the values associated with 'item', and how to convert this information into the strings you need.

    >>> ' separator '.join('this is a hint'.split())
    'this separator is separator a separator hint'
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2015
    Posts
    35
    Rep Power
    4
    If do want to include tag in my output ,how the code looks like.Please advise .Thanks for your time
  20. #11
  21. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,951
    Rep Power
    509
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo