How to stop Java from separating JSON string passed as a command line argument into separate subunits of args[]
Asked Answered
S

6

7

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!

Savagism answered 30/5, 2014 at 15:38 Comment(2)
It's not Java doing that, it's the Windows/whatever command processor.Pittel
You have to escape the json: #3020594Wadleigh
S
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.
Saki answered 30/5, 2014 at 16:17 Comment(2)
Your solution may be a better approach, but the performances are reduced, since you're doing 2 system calls to write/read a file.Annoyance
Passing a file is much more complicated, if not impossible in certain cases, when dealing with an automation of java application execution.Kauffman
N
4

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.

Nottage answered 30/5, 2014 at 16:6 Comment(1)
Simple yet powerful answer.Albanian
G
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.

Glume answered 30/5, 2014 at 16:5 Comment(2)
I'm invoking the Java program from a Python program. How do I use subprocess.call?Savagism
@adam-hopper It worked for me thanks for suggest single quote suggestion for bash shell.Tarratarradiddle
K
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.

Kauffman answered 1/6, 2021 at 7:23 Comment(0)
V
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);
Vituperation answered 18/9, 2016 at 16:17 Comment(0)
L
0
#!/bin/bash

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

Lazor answered 11/1, 2021 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.