Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7707953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:29:16+00:00 2026-06-01T00:29:16+00:00

I have a JSON feed data with lots of user relation in it such

  • 0

I have a JSON feed data with lots of user relation in it such as:

"subject_id = 1, object_id = 2, object = added 
subject_id = 1, object_id = 2, object = liked
subject_id = 1, object_id = 3, object = added
subject_id = 2, object_id = 1, object = added"

Now I’ve used following code to convert JSON to networkx Graph:

def load(fname):
G = nx.DiGraph()
d = simplejson.load(open(fname))
for item in d:
    for attribute, value in item.iteritems():
        G.add_edge(value['subject_id'],value['object_id'])
return G

And the result is something like:

[('12820', '80842'), ('12820', '81312'), ('12820', '81311'), ('13317', '29'), ('12144', '81169'), ('13140', '16687'), ('13140', '79092'), ('13140', '78384'), ('13140', '48715'), ('13140', '54151'), ('13140', '13718'), ('13140', '4060'), ('13140', '9914'), ('13140', '32877'), ('13140', '9918'), ('13140', '4740'), ('13140', '47847'), ('13140', '29632'), ('13140', '72395'), ('13140', '48658'), ('13140', '78394'), ('13140', '4324'), ('13140', '4776'), ('13140', '78209'), ('13140', '51624'), ('13140', '66274'), ('13140', '38009'), ('13140', '80606'), ('13140', '13762'), ('13140', '28402'), ('13140', '13720'), ('13140', '9922'), ('13303', '81199'), ('13130', '70835'), ('13130', '7936'), ('13130', '30839'), ('13130', '11558'), ('13130', '32157'), ('13130', '2785'), ('13130', '9914'), ('13130', '73597'), ('13130', '9918'), ('13130', '49879'), ('13130', '62303'), ('13130', '64275'), ('13130', '48123'), ('13130', '8722'), ('13130', '43303'), ('13130', '39316'), ('13130', '78368'), ('13130', '28328'), ('13130', '57386'), ('13130', '30739'), ('13130', '9922'), ('13130', '71464'), ('13130', '50296'), ('12032', '65338'), ('13351', '81316'), ('13351', '16926'), ('13351', '80435'), ('13351', '79086'), ('12107', '16811'), ('12107', '70310'), ('12107', '10008'), ('12107', '25466'), ('12107', '36625'), ('12107', '81320'), ('12107', '48912'), ('12107', '62209'), ('12816', '79526'), ('12816', '79189'), ('13180', '39769'), ('13180', '81319'), ('12293', '70918'), ('12293', '59403'), ('12293', '76348'), ('12293', '12253'), ('12293', '65078'), ('12293', '61126'), ('12293', '12243'), ('12293', '12676'), ('12293', '11693'), ('12293', '78387'), ('12293', '54788'), ('12293', '26113'), ('12293', '50472'), ('12293', '50365'), ('12293', '66431'), ('12293', '29781'), ('12293', '50435'), ('12293', '48145'), ('12293', '79170'), ('12293', '76730'), ('12293', '13260'), ('12673', '29'), ('12672', '29'), ('13559', '9327'), ('12583', '25462'), ('12252', '50754'), ('12252', '11778'), ('12252', '38306'), ('12252', '48170'), ('12252', '5488'), ('12325', '78635'), ('12325', '4459'), ('12325', '68699'), ('12559', '80285'), ('12559', '78273'), ('12020', '48291'), ('12020', '4498'), ('12746', '48916'), ('13463', '56785'), ('13463', '47821'), ('13461', '80790'), ('13461', '4425'), ('12550', '48353')]

What I want to do is I want to increase weight if there are more than 1 relation between these users. So, as I demonstrated in JSON relation, subject_id 1 has 3 relations with subject_id 2 therefore their weight should be 3 whereas user 3 has only 1 relation with subject_id 1 and so it should be 1 as weight.

Update:

I suppose I’ve solved my problem with using:

def load(fname):
G = nx.MultiDiGraph()
d = simplejson.load(open(fname))
for item in d:
    for attribute, value in item.iteritems():
        if (value['subject_id'], value['object_id']) in G.edges():
            data = G.get_edge_data(value['subject_id'], value['object_id'], key='edge')
            G.add_edge(value['subject_id'], value['object_id'], key='edge', weight=data['weight']+1)
        else:
            G.add_edge(value['subject_id'], value['object_id'], key='edge', weight=1)

print G.edges(data=True)

But still your help would be nice about improving.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:29:17+00:00Added an answer on June 1, 2026 at 12:29 am

    You can simply use the weight attribute store your weights. You can check if an edge exists with has_edge method. Combining these would give you:

    def load(fname):
        G = nx.DiGraph()
        d = simplejson.load(open(fname))
        for item in d:
            for attribute, value in item.iteritems():
                subject_id, object_id = value['subject_id'], value['object_id']
                if G.has_edge(subject_id, object_id):
                    # we added this one before, just increase the weight by one
                    G[subject_id][object_id]['weight'] += 1
                else:
                    # new edge. add with weight=1
                    G.add_edge(subject_id, object_id, weight=1)
        return G
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app pulls a json data feed , I have just added the reachability
I have a mapView that has annotation added through JSON (feed is stored in
I have a JSON feed (which I don't directly control, I have to ask
I have a news feed where items in the feed are created from JSON
I have an ASP.NET web app that places several string data into an object
I have a series of Moose objects that I'm looking to feed to JSON::XS
Hi, i have been having some problems using JSON data in flash builder lately
I have a json feed from zoho : here , you can acces the
So I have an app that downloads and parses a JSON feed with some
I am using Ember.js to create a table from a json feed. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.