0

I h've two strings got from an api url

pagehandle = urllib2.urlopen(ariel_url+"?%s"  % params1)
data1 = pagehandle.read(); 
pagehandle = urllib2.urlopen(ariel_url+"?%s"  % params2)
data2 = pagehandle.read(); 

data1 and data2 contain the following string

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180]]}

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["iphone", 49],["ipod", 590]]}

I want to convert these two strings into json and combine (or combine strings and then convert) in such a way that output json may like

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180],["iphone", 49],["ipod", 590]]]}

How can i do this? i prefer to use simplejson library

3
  • Why would you use any JSON library? Python is quite competent at handling its own datatypes. Commented Mar 10, 2011 at 9:24
  • 2
    the value property name should be quoted, right?! Commented Mar 10, 2011 at 9:33
  • It would help ENORMOUSLY if you said where/how you got those two "strings": are they already JSON strings (in which case "convert ... into json" needs explanation) or are they repr(a_Python_dict) (in which case why don't you omit the repr() and just use the dicts?) or something else ... Commented Mar 10, 2011 at 9:38

2 Answers 2

3

These are not strings, but dictionaries. You can combine those dictionary like this:

def combine(dict1, dict2):
    if dict1['priority'] == dict2['priority'] and dict1['titles'] == dict2['titles']:
        return {
            'priority': dict1['priority'],
            'titles': dict1['titles'],
            'values': dict1['values'] + dict2['values']
        }

after that you simply run:

import json
json.dumps(combine(dict1, dict2))

and you'll get a json of those two combined dictionaries.

EDIT

So I understand is what you really got is:

s1 = '{"priority":"HIGH", "titles":["Brand", "Likes"], values:[["addidas", 130],["nike", 180]]}'

s2 = '{"priority":"HIGH", "titles":["Brand", "Likes"], values:[["iphone", 49],["ipod", 590]]}'

In this case you can run:

dict1 = json.loads(s1)
dict2 = json.loads(s2)
result = combine(dict1, dict2)
Sign up to request clarification or add additional context in comments.

5 Comments

sorry these are strings got from a remote URL and the Python native dictionaries
@Mithun P: Edit your question with that information. Please also tell us how you can get each string from a remote URL and from "the Python native dictionaries" simultaneously.
Your combine() function is equivalent to lambda combine dict1, dict2: None
@John Machin: True, I have added a return. Thanks.
@John Machin, sorry both are strings, i missed the 'not' in my comment. Read "sorry these are strings got from a remote URL and NOT the Python native dictionaries" - can somebody edit that and correct ..?
3

Here you go:

import json
a = json.loads('{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180]]}')
b = json.loads('{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["iphone", 49],["ipod", 590]]}')

for value in a['values']:
   b['values'].append(value)

# edited
json.dumps(b)

1 Comment

you missed the json.dumps(b) part ;-)

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.