Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
json2table
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Recolic
json2table
Commits
8d601ad0
There was an error fetching the commit references. Please try again later.
Commit
8d601ad0
authored
4 years ago
by
Bensong Liu
Browse files
Options
Downloads
Patches
Plain Diff
add json array decay. much more powerful now!
parent
f67b0d02
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
json2table.cc
+9
-1
9 additions, 1 deletion
json2table.cc
with
9 additions
and
1 deletion
json2table.cc
+
9
−
1
View file @
8d601ad0
...
...
@@ -29,9 +29,14 @@ inline string json_to_string(json j) {
return
program_mode
?
j
.
dump
()
:
"UNKNOWN"
;
}
void
json_decay_single_element_array
(
json
&
input
)
{
while
(
input
.
is_array
()
&&
input
.
size
()
==
1
)
input
=
input
[
0
];
}
void
naive_json_access_path
(
json
&
input
,
rlib
::
string
json_path
)
{
for
(
auto
&
next
:
json_path
.
split
(
'/'
))
{
if
(
!
next
.
empty
())
{
json_decay_single_element_array
(
input
);
if
(
input
.
is_object
())
{
// Simplest case.
input
=
input
[
next
];
...
...
@@ -40,6 +45,7 @@ void naive_json_access_path(json &input, rlib::string json_path) {
// Do this for every element.
json
result_json_arr
=
json
::
array
();
for
(
auto
&
[
_
,
item
]
:
input
.
items
())
{
json_decay_single_element_array
(
item
);
if
(
item
.
is_object
())
result_json_arr
.
push_back
(
item
[
next
]);
else
...
...
@@ -57,7 +63,7 @@ void naive_json_access_path(json &input, rlib::string json_path) {
int
main
(
int
argc
,
char
**
argv
)
{
rlib
::
opt_parser
args
(
argc
,
argv
);
if
(
args
.
getBoolArg
(
"-h"
,
"--help"
))
{
rlib
::
println
(
"json2table version 1.0.
2
, maintainer Recolic Keghart <root@recolic.net>"
);
rlib
::
println
(
"json2table version 1.0.
3
, maintainer Recolic Keghart <root@recolic.net>"
);
rlib
::
println
(
"Usage: cat xxx.json | json2table"
);
rlib
::
println
(
"Usage: curl https://myapi/getJson | json2table /path/to/subobject"
);
rlib
::
println
(
"Set --programming to make the output easier for program to process. "
);
...
...
@@ -75,8 +81,10 @@ int main(int argc, char **argv) {
vector
<
string
>
headers
;
vector
<
vector
<
string
>>
rows
(
1
);
size_t
curr_row_pos
=
0
;
json_decay_single_element_array
(
input
);
if
(
input
.
is_array
())
{
for
(
auto
&
[
_
,
item
]
:
input
.
items
())
{
json_decay_single_element_array
(
item
);
if
(
item
.
is_object
())
{
// Perfect schema: [{}, {}, ...]
for
(
auto
&
[
key
,
value
]
:
item
.
items
())
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment