1

I have a JSON generated by Papa Parse from a CSV file that look like this:

{ object, object, object, .... , object }

I need to modify it so it looks like this:

{ "publication" : 
    {
      "id" : 1,
      "name" : "name1"
    },
  "publication" :
    {
      "id" : 2,
      "name" : "name2"
    },
  "publication" :
    {
      "id" : 3,
      "name" : "name3"
    }
}

Where the "id" and "name" are properties within each object.

In other words, I need a JSON that say "publication" and each "publication" occurrence have nested under it the properties names and values of one of the objects above.

How could I achieve this with Javascript?

Thanks

Samuel Segal
  • 423
  • 1
  • 7
  • 18
  • 1
    Did you try anything? – Mitya Apr 01 '15 at 22:21
  • 6
    Described structure is not possible. You can only have one property with the key Publication. – dfsq Apr 01 '15 at 22:22
  • You can't have several things with the same key. – tadman Apr 01 '15 at 22:22
  • It wouldn't make sense to have things with the same key since there would be ambiguity ... what would `data.publication` return? name1? name2? – Explosion Pills Apr 01 '15 at 22:23
  • http://stackoverflow.com/questions/4935632/parse-json-in-javascript - you would parse your json, iterate through it and rebuild it as needed. – Kai Qing Apr 01 '15 at 22:24
  • 1
    +1 to @dfsq. You are probably looking for an array that looks something like `{publications : [ { "id": 1, ...}, ... ] }` – Pevara Apr 01 '15 at 22:26
  • Ok. I'm really new to all of this. Maybe my system can accept to have all the objects under one key "publication". I will try this construct. – Samuel Segal Apr 01 '15 at 22:27

3 Answers3

2

you cannot put many keys with same name...

a solution it's put in array

{ 
    "publication" : [
      {
        "id" : 1,
        "name" : "name1"
      },
      {
        "id" : 2,
        "name" : "name2"
      },
      {
        "id" : 3,
        "name" : "name3"
      }]
}
Luan Castro
  • 1,184
  • 7
  • 14
1

Is this not an array of publications?

   { publications: [{"id":1, "name":"name1"},
         {"id":2, "name":"name2"}.....]}
ste-fu
  • 6,879
  • 3
  • 27
  • 46
1

If I understand it right, you probably mean that you have objects like this:

{"publication" : { "id" : 1, "name" : "name1" }
{"publication" : { "id" : 2, "name" : "name2" }
{"publication" : { "id" : 3, "name" : "name3" }

What you can do in order to have:

{ "publication" : [ 
    {
        "id" : 1, 
        "name" : "name1"
    }, 
    {
        "id" : 1, 
        "name" : "name2"
    }, 
    {
        "id" : 3, 
        "name" : "name3"
    } ] 
}

is:

var json = [{"publication" : { "id" : 1, "name" : "name1" }},
                {"publication" : { "id" : 2, "name" : "name2" }},
                {"publication" : { "id" : 3, "name" : "name3" }}];

var newJson = {"publication" : []};

    var i = 0;
    for (var item in json) {
        var key = json[item];
        var obj = {"id" : key["publication"]["id"], "name" : key["publication"]["name"]};
        newJson["publication"][i] = obj;
        i++;
    }

You can check this if you want to print "pretty":

How can I pretty-print JSON using JavaScript?

Community
  • 1
  • 1
caiolopes
  • 561
  • 8
  • 14