You can achieve this by using built-in json module
import json
arrayJson = json.dumps([{"email": item} for item in pyList])
Answer from ë.. on Stack OverflowYou can achieve this by using built-in json module
import json
arrayJson = json.dumps([{"email": item} for item in pyList])
Try to Google this kind of stuff first. :)
import json
array = [1, 2, 3]
jsonArray = json.dumps(array)
By the way, the result you asked for can not be achieved with the list you provided.
You need to use python dictionaries to get json objects. The conversion is like below
Python -> JSON
list -> array
dictionary -> object
And here is the link to the docs https://docs.python.org/3/library/json.html
How to convert nested list into JSON?
C# object to JSON string
Convert string List data to json format
convert JSON list to dictionary
Videos
Just adding onto alexce's response, you can easily convert the restructured data into JSON:
import json
json.dumps(result)
There are some potential security concerns with top-level arrays. I'm not sure if they're still valid with modern browsers, but you may want to consider wrapping it in an object.
import json
json.dumps({'results': result})
To solve this, you need to split the input list into chunks, by 7 in your case. For this, let's use this approach. Then, use a list comprehension producing a list of dictionaries:
>>> from pprint import pprint
>>> l = [['String 1'],['String 2'],['String 3'],['String 4'],['String 5'],
... ['String 6'],['String 7'],['String 8'],['String 9'],['String 10'],
... ['String 11']]
>>> def chunks(l, n):
... """Yield successive n-sized chunks from l."""
... for i in range(0, len(l), n):
... yield l[i:i+n]
...
>>>
>>> result = [{"title%d" % (i+1): chunk[i][0] for i in range(len(chunk))}
for chunk in chunks(l, 7)]
>>> pprint(result)
[{'title1': 'String 1',
'title2': 'String 2',
'title3': 'String 3',
'title4': 'String 4',
'title5': 'String 5',
'title6': 'String 6',
'title7': 'String 7'},
{'title1': 'String 8',
'title2': 'String 9',
'title3': 'String 10',
'title4': 'String 11'}]
I know that, list can be converted into JSON by using json.dumps(mylist).
But how can I convert something like this into JSON ?
[["abc", "bcd", "cde"] , ["pgr", "xyz"]]
In ReturnData make the members public. When I do I get:
{"Type":"type1","Items":[{"Name":"test1","Value":"result1"},{"Name":"test2","Value":"result2"}]}
So you almost had it and now you do have it.
A .NET Fiddle for this is at ObjectToJSON.
When there are internal properties mark them as a JsonProperty, otherwise go with SimpleSamples recommendation.
using System.Collections.Generic;
using System.Diagnostics;
using Newtonsoft.Json;
namespace ObjectToJson
{
class Program
{
static void Main(string[] args)
{
var i1 = new Item
{
Name = "test1",
Value = "result1"
};
var i2 = new Item
{
Name = "test2",
Value = "result2"
};
ReturnData returnData = new ReturnData();
var items = new List {i1, i2};
returnData.Type = "type1";
returnData.Items = items;
var json = JsonConvert.SerializeObject(returnData, Formatting.Indented);
Debug.WriteLine(json);
}
}
class ReturnData
{
[JsonProperty]
internal string Type { get; set; }
[JsonProperty]
internal List Items { get; set; }
}
public class Item
{
public string Name { get; set; }
public string Value { get; set; }
}
}
Output
{
"Type": "type1",
"Items": [
{
"Name": "test1",
"Value": "result1"
},
{
"Name": "test2",
"Value": "result2"
}
]
}