0

I have a JSON File as follows :

{
 "IP Address" : " 192.168.43.221 ",
 "OS ":" Ubuntu ",
 "OS Version ": " 16.04.4 ",
 "OS Installed Data ": " Dec 8 23:59 ",
 "Model name ": " AMDA10-5750MAPU with Radeon(tm)HD ",
 "MemTotal ": " 5275728kB ",
 "Hard disk capacity ": " [1.00TB] ", 
 "Users ": " ananth "
}

I want to convert the above JSON File into JSON Object only using shell scripting? Can someone help me to do this..

Thanks in advance.

6
  • What do you think a "JSON object" is, in this context? The whole concept of an "object" only makes sense in the context of runtime storage in the memory of a runtime that has some kind of object-oriented representation/abstraction. Commented Apr 3, 2018 at 5:57
  • What do you mean, "given JS"? Do you mean you have some JavaScript code you want to evaluate that object against? Then this is a question about a JavaScript runtime (node.js or whichever other one you're using), not about bash. BTW, for general purposes related to extracting content from JSON for shell use, I'd suggest jq, being the common/accepted tool for the job. Commented Apr 3, 2018 at 6:02
  • My requirement is to send the system information to WebApi and I gathered that information and generated to a JSON file. But every one hour it has to be verified for any updates/changes.so I attached that script file to crontab and I thought that object is required for verifying updates in that information. Commented Apr 3, 2018 at 6:08
  • A JSON "object" doesn't have a different serialization mechanism than the bytes in a JSON "file" -- in terms of how you send them over the wire to an API, it's the same thing either way. Commented Apr 3, 2018 at 6:09
  • 1
    The above is a JSON object. Commented Apr 3, 2018 at 6:44

1 Answer 1

1

Your file is valid as JSON text, as can for example be verified by pasting the contents into https://jsonlint.com

One way to "load" the JSON file into a javascript interpreter would be as follows. For the sake of specificity, assume the interpreter is v8 or js (JavaScript-C). First, copy the file while prepending something like "x=", e.g.

(echo "x="; cat input.json) > input.js

Now, after starting v8 or js, run: load("input.js")

The variable x will then contain the JSON object.

If you want to trim the key names so that they do not have exterior spaces, you could, for example, run the following jq command:

jq 'with_entries(.key|=(sub("^ +";"")|sub(" +$";"")))' input.json

Object Equality

Based on some comments, it appears you want to check whether the JSON objects in two files are "equal" in the sense of object-equality.

This can be done in a variety of ways. One simple and tidy way would be to use jq as follows:

jq -s '.[0] == .[1]' file1.json file2.json

An alternative would be to write the files in a "canonical" (i.e., normalized) way so that a text-oriented tool such as diff can be used, e.g.

jq -S . file1.json | sponge file1.json
jq -S . file2.json | sponge file2.json

The -S option causes the keys to be sorted in a fixed order; sponge is inessential here but convenient.

Sign up to request clarification or add additional context in comments.

2 Comments

Very thanks for taking your time and answering. But I do not want to use any other packages such as jq. I want to convert the given type JSON file into JSON Object
As already mentioned, it is a JSON object already. If you want a "canonical" textual representation of the JSON text, then again a tool like jq is perfect. You can also use jq to compare two JSON files to see if they represent the same JSON object (in the sense of object-equality).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.