6

I want to pass this JSON String to a Java class through command line arguments.

{"body": "We should definitely meet up, man", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", "location": "Boca Raton", "subject": "This is the subject of our meeting."}

However, at every space, the String gets split up. So args[0] is

{"body":

args[1] is

"We

etc.

I want args[0] to just be

{"body": "We should definitely meet up, man", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", "location": "Boca Raton", "subject": "This is the subject of our meeting."}

I tried using double quotes, but since there are quotes in the JSON string it didn't work.

How can I do this? Thanks so much!

Ben Sandler
  • 2,223
  • 5
  • 26
  • 36

6 Answers6

10

Here is a better solution:

  1. Stop passing json as a command line argument.
  2. Put the json in a file.
  3. Pass the name of the json file as a command line argument.
  4. Read the json file in your application.
DwB
  • 37,124
  • 11
  • 56
  • 82
  • Your solution may be a better approach, but the performances are reduced, since you're doing 2 system calls to write/read a file. – Maxime Flament Sep 25 '17 at 17:00
  • 1
    Passing a file is much more complicated, if not impossible in certain cases, when dealing with an automation of java application execution. – Barak BN May 06 '21 at 06:40
3

You need to escape the double-quote characters and surround the entire String with normal double-quotes:

java Test "{\"body\": \"We should definitely meet up, man\", \"startDate\": \"2014-05-29 11:00:00\", \"endDate\": \"2014-05-29 12:00:00\", \"location\": \"Boca Raton\", \"subject\": \"This is the subject of our meeting.\"}"

This is a Windows example.

Edwin Torres
  • 2,774
  • 1
  • 13
  • 15
2

This isn't a Java issue: it's a shell issue. So the answer depends on what shell you're using.

If you're on a UNIX-y shell, try putting the JSON within single quotes. For instance: java my.MainClass '{ "key1": "value1" }'. That'll probably work on Windows, too ... I'm not sure.

If you have a ' in your JSON, things get complicated. If your shell is Bash, one option is to replace every ' with '\''.

But where is the JSON coming from? If you're actually invoking this Java program from within another program, you can skip the shell altogether. Python's subprocess.call, Ruby's IO.popen, Bash's "$@" and Java's ProcessBuilder all accept an array of command-line arguments instead of a single-string command. Alternatively, logic like Ruby's Shellwords exists for just about any programming language and quotes command-line parameters so the shell parses them into exactly the bytes you specify.

Another alternative: you can pipe the JSON to your program. Invoke it like this (on UNIX):

cat | java my.MainClass
{
  "you can just type": "this stuff",
  "and it will eventually get picked up by": "Java"
}
[Ctrl-d (UNIX) or Ctrl-z (Windows)]

And read it in Java as discussed at Read/convert an InputStream to a String -- using System.in as the InputStream.

1

Try to encode your JSON string in base64:

{"body": "We should definitely meet up, man", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", "location": "Boca Raton", "subject": "This is the subject of our meeting."}

will be encoded to

eyJib2R5IjogIldlIHNob3VsZCBkZWZpbml0ZWx5IG1lZXQgdXAsIG1hbiIsICJzdGFydERhdGUiOiAiMjAxNC0wNS0yOSAxMTowMDowMCIsICJlbmREYXRlIjogIjIwMTQtMDUtMjkgMTI6MDA6MDAiLCAibG9jYXRpb24iOiAiQm9jYSBSYXRvbiIsICJzdWJqZWN0IjogIlRoaXMgaXMgdGhlIHN1YmplY3Qgb2Ygb3VyIG1lZXRpbmcuIn0=

This eliminated spaces and other troublesome characters.

Of course, you will need to decode it back inside your program, but this is relatively easy to do.

Barak BN
  • 442
  • 6
  • 14
0

The shell is removing the double quotes. Instead, you could use single quotes in the json, e.g

{'networks':{'privateIP':'10.10.1.17','publicIP':'192.168.1.144'}}

Then parse it using:

ObjectMapper om = new ObjectMapper();
om.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
Networks n = om.readValue(args[0], Networks.class);
Gapmeister66
  • 846
  • 9
  • 14
0
#!/bin/bash

JSON='
{
    "bbb":456,
    "aaaa": "SDFDS"
}
'
JSON=$(echo $JSON | sed s/\"/\\\"/g)
echo $JSON
java -Dmyparam="$JSON" Abc

oat
  • 72
  • 3