0

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 .mapto 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"
            }
        ]}
    ]
}
4
  • I tried .map but i dont get it to work. Show what you've tried. Commented Feb 22, 2019 at 9:17
  • I need this JSON... This expected json is not valid Commented Feb 22, 2019 at 9:19
  • The JSON you are saying you need would be invalid. That would make it an array with key/value mappings inside it. You would also have to wrap MH in an object. Commented Feb 22, 2019 at 9:21
  • i edited the JSON now it is valid Commented Feb 22, 2019 at 9:23

5 Answers 5

1

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"
        }
      ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, exactly what i need. That solutions works fine for me :)
1

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)

Comments

0

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)

Comments

0

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);

Comments

0

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"
      }
    }
  }
}

} }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.