Skip to content

Request: vary put values #202

Closed
Closed

Description

@xxxserxxx

Is your feature request related to a problem? Please describe.
I often use dasel to mangle test data, and would like to be able to vary the values that I'm puting into the output.

Describe the solution you'd like
I'd like to be able to specify a node selector string in the value.

cat input.json | dasel put -p json string -m -V '"Item" .[]' '.Items.[*].Name'

Where -V (or some other flag) tells dasel to run the query on the current node and use the result as the value. This example would set the attribute "Name": "Item <index>" for each Item object in the Items array.

Describe alternatives you've considered
If the set is large enough, I'd write a Ruby or awk script. My test sets tend to be small, so I usually just edit them by hand.

I also considered using --merge-input-documents, but I don't believe it'd replace the existing objects -- just add to them. In any case, it'd be fairly involved to create the merge document, and possibly more trouble than writing code to modify the data.

Additional context
I'd like to turn this:

{"name": "Real data",
 "things": {
   {"title": "foo", "id": 4843},
   {"title": "top secret", "id": 4448},
   {"title": "blah", "id": 3333}
}}

into this:

{"name": "Fake data",
 "things": {
   {"title": "thing 1", "id": 1},
   {"title": "thing 2", "id": 2},
   {"title": "thing 3", "id": 3}
}}

Activity

  1. TomWright commented on Mar 6, 2022

    @TomWright
    Owner

    This is an interesting use-case.

    Note to myself - This could be achieved with the --format flag if the selector was made available in the format templates here:

    return template.FuncMap{

  2. xxxserxxx commented on Mar 8, 2022

    @xxxserxxx
    Author

    Being able to insert values relative to the document would, I think, be a more interesting generally, but for my use case it'd be sufficient if I could call a program (or shell function) for each node. I could generate values externally (if there was a mechanism for it), but (as I said) I imagine it might be more valuable in general to be able to, e.g., set a value from some permutation of another attribute in the node. E.g., set the Name attribute to the equivalent of template-ish "{{.FirstName}} {{.LastName}}"

    To be clear, I like the direction you're thinking in. It's frankly better than what I was imagining.

  3. TomWright commented on Dec 10, 2025

    @TomWright
    Owner

    I believe this is already achievable with the updated query/selector syntax in dasel V3.
    We're many years later so I will close this now, but any problems please create a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions