简体   繁体   中英

Adding Square Brackets to JSON-Object

I get this JSON from my server. But to work with this JSON i need to Add Square Brackets to the MH Object. How can i do that. I tried .map but i dont get it to work for me. Is there any better solution. Or is .map to use there. If yes can you show me a hint how to do that. Or is there a better solution?

{
    "PAD": [
        {
            "PADPS286": "Dampf",
            "PADPS124": "Hans",
            "PADPS60": "2018-05-01",
            "PADPS143": "1",
            "MH": {
                "MHVSS1": [
                    {}
                ],
                "MHDIRW214": 2017,
                "MHDIRW215": 2018,
                "birthdate": "2018-05-01",
                "MHDIRW129 ": "0"
            }
        },
        {
            "PADPS286": "Snow",
            "PADPS124": "Jon",
            "PADPS60": "2077-05-01",
            "PADPS143": "",
            "MH": {
                "MHVSS1": [
                    {}
                ],
                "MHDIRW214": 4,
                "MHDIRW215": 4,
                "birthdate": "2077-05-01",
                "MHDIRW129 ": "0"
            }
        }
    ]
}

I need this JSON with sqare Brackets arround teh MH Object

{
    "PAD": [
        {
            "PADPS286": "Dampf",
            "PADPS124": "Hans",
            "PADPS60": "2018-05-01",
            "PADPS143": "1",
            "MH": [{
                "MHVSS1": [
                    {}
                ],
                "MHDIRW214": 2017,
                "MHDIRW215": 2018,
                "birthdate": "2018-05-01",
                "MHDIRW129 ": "0"
            }]
        },
        {
            "PADPS286": "Snow",
            "PADPS124": "Jon",
            "PADPS60": "2077-05-01",
            "PADPS143": "",
            "MH": [{
                "MHVSS1": [
                    {}
                ],
                "MHDIRW214": 4,
                "MHDIRW215": 4,
                "birthdate": "2077-05-01",
                "MHDIRW129 ": "0"
            }
        ]}
    ]
}

It's not really "adding square brackets", it's wrapping the "MH" object in an array.

Anyway, here's a .map statement that will do it for you (without mutating the original data, hence the Object.assign shenanigans):

data.PAD = data.PAD.map((padObj) => Object.assign({}, padObj, {MH: [padObj.MH]}));

Basically, for each entry in the PAD array, we're merging three objects there:

  • a fresh empty object {}
  • the original padObj entry
  • a small object that only has the MH element from the original padObj wrapped in an array.

The output is as expected:

{
  "PAD": [
    {
      "PADPS286": "Dampf",
      "PADPS124": "Hans",
      "PADPS60": "2018-05-01",
      "PADPS143": "1",
      "MH": [
        {
          "MHVSS1": [{}],
          "MHDIRW214": 2017,
          "MHDIRW215": 2018,
          "birthdate": "2018-05-01",
          "MHDIRW129 ": "0"
        }
      ]
    },
    {
      "PADPS286": "Snow",
      "PADPS124": "Jon",
      "PADPS60": "2077-05-01",
      "PADPS143": "",
      "MH": [
        {
          "MHVSS1": [{}],
          "MHDIRW214": 4,
          "MHDIRW215": 4,
          "birthdate": "2077-05-01",
          "MHDIRW129 ": "0"
        }
      ]
    }
  ]
}

Simply use a forEach on data.PAD to reassign the MH property to an array. Since arrays and objects are passed by reference in JavaScript, this will modify you data in place:

data.PAD.forEach(pad => pad.MH = [pad.MH]);

 const data = { "PAD": [ { "PADPS286": "Dampf", "PADPS124": "Hans", "PADPS60": "2018-05-01", "PADPS143": "1", "MH": { "MHVSS1": [ {} ], "MHDIRW214": 2017, "MHDIRW215": 2018, "birthdate": "2018-05-01", "MHDIRW129 ": "0" } }, { "PADPS286": "Snow", "PADPS124": "Jon", "PADPS60": "2077-05-01", "PADPS143": "", "MH": { "MHVSS1": [ {} ], "MHDIRW214": 4, "MHDIRW215": 4, "birthdate": "2077-05-01", "MHDIRW129 ": "0" } } ] }; data.PAD.forEach(pad => pad.MH = [pad.MH]); console.log(data)

Try using forEach loop. On every MH property inside the PAD array , set it to an array , and assign it back to the object

 var a = { "PAD": [{ "PADPS286": "Dampf", "PADPS124": "Hans", "PADPS60": "2018-05-01", "PADPS143": "1", "MH": { "MHVSS1": [{}], "MHDIRW214": 2017, "MHDIRW215": 2018, "birthdate": "2018-05-01", "MHDIRW129 ": "0" } }, { "PADPS286": "Snow", "PADPS124": "Jon", "PADPS60": "2077-05-01", "PADPS143": "", "MH": { "MHVSS1": [{}], "MHDIRW214": 4, "MHDIRW215": 4, "birthdate": "2077-05-01", "MHDIRW129 ": "0" } } ] }; a.PAD.forEach((e, i) => { a.PAD[i].MH = [e.MH] }) console.log(a)

 var obj = { "PAD": [ { "PADPS286": "Dampf", "PADPS124": "Hans", "PADPS60": "2018-05-01", "PADPS143": "1", "MH": { "MHVSS1": [ {} ], "MHDIRW214": 2017, "MHDIRW215": 2018, "birthdate": "2018-05-01", "MHDIRW129 ": "0" } }, { "PADPS286": "Snow", "PADPS124": "Jon", "PADPS60": "2077-05-01", "PADPS143": "", "MH": { "MHVSS1": [ {} ], "MHDIRW214": 4, "MHDIRW215": 4, "birthdate": "2077-05-01", "MHDIRW129 ": "0" } } ] }; var objMod = {}; objMod.PAD = obj.PAD.map(o => { var mo = JSON.parse(JSON.stringify(o)); mo["MH"] = Array(o["MH"]); return mo; }); console.log(objMod);

If you want to add an array of strings (adding brackets only around the strings) then the above may work, however if you are trying to make the property an array what you need to do is cast the JSON Property as a list in the class as per:

public class AddressElements 
 implements Serializable
 {
  @JsonProperty("Street")
  private List<Street> Street = new ArrayList<Street>();
  @JsonProperty("HouseNumber")
  private List<HouseNumber> HouseNumber = new ArrayList <HouseNumber>();
  @JsonProperty("Locality")
  private List<Locality> Locality = new ArrayList<Locality>();
  @JsonProperty("AdministrativeDivision")
  private List<AdministrativeDivision> AdministrativeDivision = new 
  ArrayList<AdministrativeDivision>();
  @JsonProperty("PostalCode")
  private List<PostalCode> PostalCode = new ArrayList<PostalCode>();
  @JsonProperty("Country")
  private String Country;

  <getters and setters>
}

The resulting output (after initializing) would look like:

"body": {
"Login": "ssssss",
"Password": "eeeeee",
"UseTransactionPool": "test",
"JobToken": "",
"Request": {
"parameters": {
    "Mode": "Certified"
  },
"IO": {
    "Inputs": {
      "AddressElements": {
        "Street": [
          {
            "Value": "Wilder Rd."
          }
        ],
        "HouseNumber": [
          {
            "Value": "123"
          }
        ],
        "Locality": [
          {
            "Value": "Newton"
          }
        ],
        "AdministrativeDivision": [
          {
            "Value": "NY"
          }
        ],
        "PostalCode": [
          {
            "Value": "12345"
          }
        ],
        "Country": "USA"
      }
    }
  }
}

} }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM