We help IT Professionals succeed at work.

Convert Json to key value pair in linux

enthuguy
enthuguy asked
on
85 Views
Last Modified: 2020-06-22
HI
Could you help please

  1. Have this source json file.
  2. Would like to extract each attributes and make it single line.
  3. If an additional attributes adds to json. it would be good if it dynamically adds to single line :)
  4. This file gets generated first. Now would like to run a tiny script to convert this json to single line in this specific format.
  5. As you can see key value pair with space separated.

# Source Json File. result.json
{
  "features": 1,
  "ignored": 0,
  "efficiency": 0.44444444,
  "failures": {
    "services.Example.Shakeout.templateShakeout": "templateShakeout.feature:42}"
  },
  "totalTime": 646464.604285,
  "threads": 10,
  "scenarios": 82,
  "failed": 10,
  "passed": 72,
  "version": "0.9.6.RC2",
  "elapsedTime": 197457,
  "team": "teamA",
  "microserviceteam": "batch",
  "environment": "poc",
  "testingtype": "batch",
  "failedRate": 12,
  "passedRate": 88
}

Open in new window


# Expected output
echo 'features=1 ignored=0 efficiency=0.44444444 totalTime=646464.604285 threads=10 scenarios=82 failed=10 passed=72 version="0.9.6.RC2" elapsedTime=197457 team="teamA" microserviceteam="batch" environment="poc" testingtype="batch" failedRate=2 passedRate=98 1465839830100400200'

Open in new window


# Just for reference on how jq can be used
failedRate=$(cat result.json | jq '.failed / (.passed + .failed) * 100 | round')
passedRate=$(cat result.json | jq '.passed / (.passed + .failed) * 100 | round')

Open in new window


Thanks again
Comment
Watch Question

David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You'll use the https://stedolan.github.io/jq tool for any type of JSON transform.

Once you start using jq, you'll wonder how you ever lived without it.
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
All major Distros package jq, so just install the package + you'll be started in a few seconds.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks a lot  pepr, it worked as expected :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.