0

I want to add an array to the "fields" How can I do that?

how to add a "fields" array?

fields=()
for f in NameData/*.json
do
        Name= cat $f | jq -r '.[] | .Name'
        Value= cat $f | jq -r '.[] | .Value'
        
        fields+= #how? 
        '{
            "name" : "'$Name'",
            "value": "'$Value'"
        },'
done

#output for fields

{
    "name" : "Test"
    "value" : "1"
},
{
    "name" : "Test2"
    "value" : "2"
},
...

Because next to post

function Hi(){
    curl -H "Content-Type: application/json" -X POST -d \
        '{
                    #how to array in fields //while?
            "fields": [
                    {
                        "name": "Test",
                        "value":"'1'",
                    },
                    {
                        "name": "Test2",
                        "value":"'2'",
                    }
                    ...
                ]
        ...
}}

How do I need to give more details? Can you help me? Thanks.

My files here (JSON) like I'm trying to get the name and value

xample.json
[
    ...
    {
        "folder" : "nobody",
        "name": "1TS",
        "value": "2",
        ...
    }
]
example2.json
[
    {
        "name": "TST",
        "value": "4.75",
        ...
    }
]
tripleee
  • 175,061
  • 34
  • 275
  • 318
myshane
  • 21
  • 5
  • What are the `NameData/*.json` files like? I'm assuming something like `[{"Name":"Joe Smith", "Value":"$12.75", "OtherField":"OtherData"}]`, but will there always be exactly one array entry per file? Will it always contain both `Name` and `Value`? Or does the script need to deal with extra and/or missing entries? – Gordon Davisson Jan 25 '21 at 03:50
  • yeah, I wrote the content of my files (son) – myshane Jan 25 '21 at 06:46

2 Answers2

1

Bash arrays are just collections of strings. Probably just collect the key/value pairs and then finish up by formatting it as JSON.

fields=()
for f in NameData/*.json
do
        fields+=$(jq -r '.[] | {name: .name, value: .value}' "$f") 
done

Notice also how you need a command substitution to run a command and assign its output to a variable and how we avoid the useless cat and spend just one jq call to extract both fields.

But probably a much better solution is just to use jq directly.

curl -H "Content-Type: application/json" -X POST -d "$(
    jq -s '{ fields: map({name: .[0].name, value: .[0].value})}' NameData/*.json)"
tripleee
  • 175,061
  • 34
  • 275
  • 318
1

It looks like you can do almost all of the work in jq, like this:

fields=$(jq -n '[inputs | .[] | {name, value}]' NameData/example*.json)

Explanation: -n makes it start with a null input, then [ ] creates a new array from each of the inputs (the files specified after, i.e. the NameData/example*.json files), stripping the top-layer array and selecting out just the name and value fields.

Note that the field names are case-sensitive; in some places you used Name and Value, in other places name and value. Make sure you use the correct capitalization for the data. If you need to convert it e.g. from capital to lowercase, you can do this:

fields=$(jq -n '[inputs | .[] | {name : .Name, value : .Value}]' NameData/example*.json)

To use this with curl, you need to get the quoting right (see this question). It's probably easiest to single-quote most of the POST data, but switch to double-quoting for the $fields part:

curl -H "Content-Type: application/json" -X POST -d \
    '{
        "fields": '"$fields"',
    ...
     }'

Alternately, you could just create the whole thing in jq:

post_data=$(jq -n '{fields : [inputs | .[] | {name : .Name, value : .Value}], ... }' NameData/example*.json)
curl -H "Content-Type: application/json" -X POST -d "$post_data" ...
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151