Search code examples
javajsonstringmultilinestringjsonconverter

JSON to Multiline Java String Converter


This great codebeautify almost has what I need, but does not escape special characters and uses single quotes. Seems okay for JavaScript.

Any tools out there that can convert a JSON payload a to multi line escaped Java string?

I want to produce something this:

"{\r\n" +
    "\"AttributeLogicalName\": \"entityLogicalname\",\r\n" +
    "\"EntityLogicalName\": \"entity\",\r\n" +
    "\"Value\": \"100000002\",\r\n" +
    "\"Label\": {\r\n" +
        "\"LocalizedLabels\": [\r\n" +
            "{\r\n" +
                "\"Label\": \"nerd\",\r\n" +
                "\"LanguageCode\": 1033,\r\n" +
                "\"IsManaged\": false,\r\n" +
                "\"MetadataId\": \"881daca2-5c68-e911-a825-000d3a1d501d\",\r\n" +
                "\"HasChanged\": null\r\n" +
            "}\r\n" +
        "],\r\n" +

But codebeautify only produces this.

 '   {  '  + 
 '       "AttributeLogicalName": "new_localoptionsettoform",  '  + 
 '       "EntityLogicalName": "cr965_testcdsentity",  '  + 
 '       "Value": "100000002",  '  + 
 '       "Label": {  '  + 
 '           "LocalizedLabels": [  '  + 
 '               {  '  + 
 '                   "Label": "nerd",  '  + 
 '                   "LanguageCode": 1033,  '  + 
 '                   "IsManaged": false,  '  + 
 '                   "MetadataId": "881daca2-5c68-e911-a825-000d3a1d501d",  '  + 
 '                   "HasChanged": null  '  + 
 '               }  '  + 
 '           ],  '  + 

Solution

  • You can do this in IntelliJ IDEA.

    Copy your JSON to a formatter to format it, so that it appears pretty-print instead of single line:

    [
      {
        "_id": "5cc3f7e46a4fe0f0fa9d4084",
        "index": 0,
        "guid": "1d431fc7-8ec1-477b-8a22-d21f2169a474",
        "isActive": true,
        "age": 29,
        "name": "Dixon Downs",
        "gender": "male",uino, Louisiana, 4533",
        "about": "Ut nostrud consectetur eiusmod est eiusmod sit commodo nulla minim magna. Anim esse fugiat et quis ullamco aliquip enim. Excepteur laboris laboris proident elit aliqua ullamco quis ut reprehenderit et aliquip dolore id labore. Mollit officia quis ullamco mollit. Veniam laborum ex elit ut veniam sunt ullamco ad cupidatat ullamco in. Occaecat in irure excepteur elit pariatur ex ex elit adipisicing occaecat minim.\r\n",
        "tags": [
          "laborum",
          "consectetur",
          "occaecat",
          "duis",
          "dolore",
          "sit",
          "aute"
        ],
        "friends": [
          {
            "id": 0,
            "name": "Owen Kinney"
          },
          {
            "id": 1,
            "name": "Wiggins Marks"
          },
          {
            "id": 2,
            "name": "Maura Lara"
          }
        ]
      }
    ]
    

    Then, write a string literal in IntelliJ IDEA:

    ""
    

    and put the cursor between the two quotes, and paste. You'll see:

        "[\n" +
                "  {\n" +
                "    \"_id\": \"5cc3f7e46a4fe0f0fa9d4084\",\n" +
                "    \"index\": 0,\n" +
                "    \"guid\": \"1d431fc7-8ec1-477b-8a22-d21f2169a474\",\n" +
                "    \"isActive\": true,\n" +
                "    \"age\": 29,\n" +
                "    \"name\": \"Dixon Downs\",\n" +
                "    \"gender\": \"male\",uino, Louisiana, 4533\",\n" +
                "    \"about\": \"Ut nostrud consectetur eiusmod est eiusmod sit commodo nulla minim magna. Anim esse fugiat et quis ullamco aliquip enim. Excepteur laboris laboris proident elit aliqua ullamco quis ut reprehenderit et aliquip dolore id labore. Mollit officia quis ullamco mollit. Veniam laborum ex elit ut veniam sunt ullamco ad cupidatat ullamco in. Occaecat in irure excepteur elit pariatur ex ex elit adipisicing occaecat minim.\\r\\n\",\n" +
                "    \"tags\": [\n" +
                "      \"laborum\",\n" +
                "      \"consectetur\",\n" +
                "      \"occaecat\",\n" +
                "      \"duis\",\n" +
                "      \"dolore\",\n" +
                "      \"sit\",\n" +
                "      \"aute\"\n" +
                "    ],\n" +
                "    \"friends\": [\n" +
                "      {\n" +
                "        \"id\": 0,\n" +
                "        \"name\": \"Owen Kinney\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"id\": 1,\n" +
                "        \"name\": \"Wiggins Marks\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"id\": 2,\n" +
                "        \"name\": \"Maura Lara\"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "]"