Issue creating nested object in jq - Stack Overflow

Im trying to write a JQ function that will take some keyvalue arguments and format them into a nested

Im trying to write a JQ function that will take some key/value arguments and format them into a nested object of various data types.

  • If the arg key has a period, then use the period as a delimiter for nested object keys. Create that object and place the value there.
  • If the value has commas in it, then assume those are array value delimiters, and create the array.

Examples:

  • --arg foo.bar baz would create {"foo":{"bar":"baz"}}
  • --arg numbers.prime 2,3,5,7,11,13 would create {"numbers":{"prime":[2,3,5,7,11,13]}}
  • --arg someint 123 would create {"someint: 123}

Content of args2json.jq:

# Cast some text into the most likely value and appropriate data type
def cast_arg:
    . as $val |
    if $val == "null" then null
    elif $val == "true" then true
    elif $val == "false" then false
    else ( $val 
        | try tonumber catch ($val
            | split(",") as $segments 
            | if ($segments|length) == 0 then null
              elif ($segments|length) == 1 then $val 
              else ($segments|map(cast_arg))
              end
        )
    ) end;
    

# Take the --arg foo.bar baz,bang,qux into {foo: {bar: [baz, bang, qux]}}
def args2json:
    . as $params 
    | to_entries 
    | reduce .[] as $entrties ([]; 
        $entrties 
        | (setpath(.key | split("."); .value | cast_arg) | del(.key) | del(.value) | $params + . )
    );

.params=($ARGS.named | args2json) 

Execution:

jq --null-input \
  --arg count 5 \
  --arg name john \
  --arg enabled false \
  --arg shouldbenull null \
  --arg mamals pig,cat,dog \
  --arg sea.creatures 'shark,crab,654,squid' \
  --from-file ./jq/modifiers/args2json.jq

I would expect to see the output:

{
  "count": 5,
  "name": "john",
  "enabled": false,
  "shouldbenull": null,
  "mamals": [
    "pig",
    "cat",
    "dog"
  ],
  "sea": {
    "creatures": [
      "shark",
      "crab",
      654,
      "squid"
    ]
  }
}

But here's the actual output

{
  "params": {
    "count": "5",
    "name": "john",
    "enabled": "false",
    "shouldbenull": "null",
    "mamals": "pig,cat,dog",
    "sea.creatures": "shark,crab,654,squid",
    "sea": {
      "creatures": [
        "shark",
        "crab",
        654,
        "squid"
      ]
    }
  }
}

The last {sea: creatures: [...]}} looks great. All values got casted properly and the creatures array is nested inside the sea object property. But it only seems to update the very last thing in the output and nothing else which I can't figure out.

Any help would be appreciated

Im trying to write a JQ function that will take some key/value arguments and format them into a nested object of various data types.

  • If the arg key has a period, then use the period as a delimiter for nested object keys. Create that object and place the value there.
  • If the value has commas in it, then assume those are array value delimiters, and create the array.

Examples:

  • --arg foo.bar baz would create {"foo":{"bar":"baz"}}
  • --arg numbers.prime 2,3,5,7,11,13 would create {"numbers":{"prime":[2,3,5,7,11,13]}}
  • --arg someint 123 would create {"someint: 123}

Content of args2json.jq:

# Cast some text into the most likely value and appropriate data type
def cast_arg:
    . as $val |
    if $val == "null" then null
    elif $val == "true" then true
    elif $val == "false" then false
    else ( $val 
        | try tonumber catch ($val
            | split(",") as $segments 
            | if ($segments|length) == 0 then null
              elif ($segments|length) == 1 then $val 
              else ($segments|map(cast_arg))
              end
        )
    ) end;
    

# Take the --arg foo.bar baz,bang,qux into {foo: {bar: [baz, bang, qux]}}
def args2json:
    . as $params 
    | to_entries 
    | reduce .[] as $entrties ([]; 
        $entrties 
        | (setpath(.key | split("."); .value | cast_arg) | del(.key) | del(.value) | $params + . )
    );

.params=($ARGS.named | args2json) 

Execution:

jq --null-input \
  --arg count 5 \
  --arg name john \
  --arg enabled false \
  --arg shouldbenull null \
  --arg mamals pig,cat,dog \
  --arg sea.creatures 'shark,crab,654,squid' \
  --from-file ./jq/modifiers/args2json.jq

I would expect to see the output:

{
  "count": 5,
  "name": "john",
  "enabled": false,
  "shouldbenull": null,
  "mamals": [
    "pig",
    "cat",
    "dog"
  ],
  "sea": {
    "creatures": [
      "shark",
      "crab",
      654,
      "squid"
    ]
  }
}

But here's the actual output

{
  "params": {
    "count": "5",
    "name": "john",
    "enabled": "false",
    "shouldbenull": "null",
    "mamals": "pig,cat,dog",
    "sea.creatures": "shark,crab,654,squid",
    "sea": {
      "creatures": [
        "shark",
        "crab",
        654,
        "squid"
      ]
    }
  }
}

The last {sea: creatures: [...]}} looks great. All values got casted properly and the creatures array is nested inside the sea object property. But it only seems to update the very last thing in the output and nothing else which I can't figure out.

Any help would be appreciated

Share Improve this question asked Nov 16, 2024 at 14:20 J HJ H 531 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

This should work:

def args2json:
  reduce to_entries[] as $e (null;
    setpath($e.key | split("."); $e.value | cast_arg)
  );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745657075a4638605.html

相关推荐

  • Issue creating nested object in jq - Stack Overflow

    Im trying to write a JQ function that will take some keyvalue arguments and format them into a nested

    21天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信