0

I want to create a json file like

{
"a":["12","34","23",...],
"b":["13","14","45",....],
.
.
.
}

key should come from the list:

lis = ['a','b',...]

and value from the sql query "select id from" + i , where I am iterating through the list through "i". This query simply returns the column id.

Here is the sample code:

lis = ['a','b','c']
len_obj = len(lis)

with open("Dataset.json", 'w') as file:
    for i in lis:
        file.write(i)
        obj_query = i + '_query'       
        obj_query = sf.query("select id from " + i) 
        jsondata = json.loads(json.dumps(obj_query['records']))
        length = len(jsondata)
        i = {}
        k = 0

        for j in range(length):
            obj_id = jsondata[j]['Id']
            # print("id " + obj_id)

            if k == 0:
                ids = "\"" + obj_id + "\""
                k = 1
            else:
                ids = ids + ",\"" + obj_id + "\""  

        if count != len_obj - 1: 
            file.write(ids)    
        else:
            file.write(ids)    

        count += 1
    file.write("}")

final output should be like:

{
"a":["12","23",...],
"b":["234","456",...],
}

This is my first blog and 1st program also. Please guide me through this.

Please forgive the indentation of the program as I am not able to write it here properly.

2
  • what is objects ?, cant see it there Commented Dec 2, 2015 at 9:54
  • Can you post an example of Dataset.json ? Commented Dec 2, 2015 at 10:00

3 Answers 3

7

You should be able to condense the whole thing down to just this:

import json

tables = ["a", "b", "c", "d"]
data = {}

for t in tables:
    results = sf.query("select id from %s" % t)["records"]
    data[t] = [r["id"] for r in results]

with open("Dataset.json", "w") as f:
    json.dump(data, f)
Sign up to request clarification or add additional context in comments.

Comments

6

You can simply create a dictionary containing the values you are after and then convert it to json using json.dumps

import json
data = {}
data['a'] = ["12","34","23"]
data['b'] = ["13","14","45"]
json_data = json.dumps(data)
print json_data

2 Comments

You can even do with open("Dataset.json", 'w') as file: json.dump( data, file ).
@Jaco i tried that but it gave me like `{"a":["12,23,45..."]} {"b":["23,45,67..."]}{...} I ma posting my code in down below.
0

@Jaco

lis = ['a','b','c']

    with open("Dataset.json", 'w') as file:

        for i in lis:

            obj_query = i + '_query'       
            obj_query = sf.query("select id from " + i) 

            jsondata = json.loads(json.dumps(obj_query['records']))
            length = len(jsondata)

            # create dict
            data1 = {}
            k = 0
            for j in range(length):
                obj_id = jsondata[j]['Id']
                # print("id " + obj_id)

                if k == 0:

                    ids =  obj_id 
                    k = 1

                else:

                    ids = ids + "," + obj_id 

                data1[i] = [ids]

                json_data = json.dumps(data1)

        file.write(json_data)

the response i got is

{"a":["12,23,34.."]}{"b":["23,45,..."]}{...}

Comments

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.