Skip to content

Commit

Permalink
added the -w flag
Browse files Browse the repository at this point in the history
  • Loading branch information
s-retlaw committed Sep 8, 2022
1 parent 821d6cc commit 93c175e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jsu"
version = "0.2.0"
version = "0.3.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,27 @@ Outputs:
]
```

Note: Extract looks for {} or [] sequences. For an array [] it must have an element that is a json object.
Note: Extract looks for {} or [] sequences. For an array [] it must have an element that is a json object. Also note the output is not standard json. use the -w to wrap in a top level property.


## To wrap the json in a new top level property
'''echo 'some text {"a":1} more "text" [1,2,3] text [1,{"b":2}] text ' | jsu -x -w extracted```

Outputs:
```
{
"extracted": [
{
"a": 1
},
[
1,
{
"b": 2
}
]
]
}
```


11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn write_output(output_file : Option<&String>, data: &str) -> Result<(), Box<dyn

fn run_cmd_line() -> Result<(), Box<dyn Error>> {
let matches = App::new("jsu")
.version("0.2.0")
.version("0.3.0")
.author("Walter Szewelanczyk")
.about("Json Utils")
.arg(Arg::new("input_file")
Expand Down Expand Up @@ -136,13 +136,19 @@ fn run_cmd_line() -> Result<(), Box<dyn Error>> {
.takes_value(false)
.action(clap::ArgAction::SetTrue)
.help("Scan the text looking for any embeded JSON objects and return them. Will return an array."))
.arg(Arg::new("wrap")
.short('w')
.long("wrap")
.help("Wrap the json with a new top level property.")
.takes_value(true))
.get_matches();

let input_file = matches.get_one::<String>("input_file");
let output_file = matches.get_one::<String>("output");
let compact = *matches.get_one::<bool>("compact").unwrap();
let expand = *matches.get_one::<bool>("expand").unwrap();
let extract = *matches.get_one::<bool>("extract").unwrap();
let wrap = matches.get_one::<String>("wrap");

let input = get_input(input_file)?;
let mut json = if extract {
Expand All @@ -153,6 +159,9 @@ fn run_cmd_line() -> Result<(), Box<dyn Error>> {
if expand {
json = expand_json_value(json);
}
if let Some(prop_name) = wrap {
json = serde_json::json!({prop_name : json});
}
let output = if compact{
serde_json::to_string(&json).map_err(|e| format!("Error converting JSON to a compact string : {e}"))?
}else{
Expand Down

0 comments on commit 93c175e

Please sign in to comment.