1
$\begingroup$

While processing data in cycle my code create some json object like this:

[
    {
        "id" : 1,
        "some_param_1" : 10,
        "some_param_2" : "some_string",
        ....
    }
]

Code generate many json objects and I would like to export them in file with this structure:

[
    {
        "id" : 1,
        "some_param_1" : 10,
        "some_param_2" : "some_string"
    },
    {
        "id" : 2,
        "some_param_1" : 20,
        "some_param_2" : "some_string"
    },
    {
        "id" : 3,
        "some_param_1" : 30,
        "some_param_2" : "some_string"
    },
    ....
]

How i can export this data, using Export[] function? I can't find answer in official documentation and here.

Maybe I should use export like string and write data string by string in export file?

$\endgroup$
  • 6
    $\begingroup$ What do you mean by "json object"? (Please edit the answer to this into your post.) Is it a string? Then export as "String". Is it nested lists and associations? Then export as "RawJSON". Does it have lists of Rules instead of associations? Then export as "JSON". $\endgroup$ – Szabolcs Jan 16 at 10:55
3
$\begingroup$

If what you are looking for is a way to combine each of the "json objects" into a single one for Export then

j1 = ImportString["[{\"id\":1,\"some_param_1\":10,\"some_param_2\":\"some_string\"}]", "JSON"];
j2 = ImportString["[{\"id\":2,\"some_param_1\":20,\"some_param_2\":\"some_string\"}]", "JSON"];

ExportString[j1~Join~j2, "JSON"]

Generates

[
    {
        "id":1,
        "some_param_1":10,
        "some_param_2":"some_string"
    },
    {
        "id":2,
        "some_param_1":20,
        "some_param_2":"some_string"
    }
]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.