LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-03-2020, 10:45 AM   #1
project.linux.proj
Member
 
Registered: Dec 2012
Posts: 80

Rep: Reputation: Disabled
POST data in json array with curl command in bash loop


I am able to POST data with curl in the JSON format with the loop in bash script

Current
$(curl -o /dev/null -s -X POST “${url}" -H "accept: application/json" -H "Authorization: ${base}” -H "Content-Type: application/json" -d “{\”data1\”:\”${data1}\”,\”data2\”:\”${data2}\”}”)

[
{
"data1": "data1",
"data2”: "data2”
},
{
"data3”: "data3”,
"data4”: "data4”
}
]


But my requirement is to achieve below format in JSON array with curl. Please help me to achieve me to append JSON with curl command with every iteration of loop.

{“Array": [
{
"data1": "data1",
"data2”: "data2”
},
{
"data3”: "data3”,
"data4”: "data4”
}
]
}
 
Old 03-03-2020, 12:40 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,712

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by project.linux.proj View Post
I am able to POST data with curl in the JSON format with the loop in bash script

Current
Code:
$(curl -o /dev/null -s  -X POST “${url}" -H "accept: application/json" -H "Authorization: ${base}” -H "Content-Type: application/json" -d “{\”data1\”:\”${data1}\”,\”data2\”:\”${data2}\”}”)

[
   {
     "data1": "data1",
     "data2”: "data2”
   },
   {
    "data3”: "data3”,
     "data4”: "data4”
    }
]
But my requirement is to achieve below format in JSON array with curl. Please help me to achieve me to append JSON with curl command with every iteration of loop.
Code:
{“Array":  [
    {
      "data1": "data1",
      "data2”: "data2”
    },
   {
      "data3”: "data3”,
      "data4”: "data4”
    }
]
}
As you were asked in other threads, use CODE tags when posting code and similar items. Also, your other (nearly duplicate) thread contains almost the same question, and like this one, lacks something. That would be, **YOUR EFFORTS** to solve your own problems. What scripts/programs have you tried to write, since this is your 'requirement'??? We are happy to help you if you're stuck, but we aren't going to write your scripts for you.

Read the "Question Guidelines" link in my posting signature.
 
Old 03-03-2020, 01:35 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,146
Blog Entries: 6

Rep: Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834Reputation: 1834
You have a mix of quotes in these examples.
Code:
[
{
"data1": "data1",
"data2”: "data2”
},
{
"data3”: "data3”,
"data4”: "data4”
}
]


{“Array": [
{
"data1": "data1",
"data2”: "data2”
},
{
"data3”: "data3”,
"data4”: "data4”
}
]
}
This one too
Code:
$(curl -o /dev/null -s -X POST “${url}" -H "accept: application/json" -H "Authorization: ${base}” -H "Content-Type: application/json" -d “{\”data1\”:\”${data1}\”,\”data2\”:\”${data2}\”}”)
Did you copy and paste some of that from a website?

Not possible to tell what you are wanting.

https://stackoverflow.com/questions/...data-with-curl
https://stackoverflow.com/questions/...-in-linux-bash
http://manpages.org/jq
https://stackoverflow.com/questions/...o-json-in-bash
 
1 members found this post helpful.
  


Reply

Tags
bash, curl, json, json jq, linux



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: JSON Lines: record-style JSON LXer Syndicated Linux News 0 01-28-2020 08:03 PM
Curl command, Json data to excel gaa100 Linux - Newbie 3 08-13-2014 06:52 AM
Curl command, Json data to excel gaa100 Linux - Newbie 1 08-12-2014 07:46 AM
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
cURL: Server has many IPs, how would I make a cURL script use those IPs to send data? guest Programming 0 04-11-2009 11:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration